按钮单击仅适用一次(Button click only works once)

编程入门 行业动态 更新时间:2024-10-15 22:28:22
按钮单击仅适用一次(Button click only works once)

我有一个按钮,一旦点击它就会在“forPassengers”div的末尾添加一个新的输入元素,但它只能工作一次。 点击一下就不再可以点击了。

控制器:

app .controller('multipleFlightsController', function ($scope, $http, $location) { $scope.addPassenger = function () { document.getElementById('forPassengers') .innerHTML += '<input class="form-control airport ng-pristine ng-untouched ng-valid" type="text" ng-model="additionalPassengers" placeholder="#" tabindex="0" aria-invalid="false"> '; }; /*..... more code*/

工作解决方案(编辑)

最终解决了我的问题的是实际使用AngularJS而不是通过document.getElementById函数访问input元素。

I have a button which appends a new input element to the end of the "forPassengers" div once it's clicked, but it only works once. After one click it's not clickable anymore.

Controller:

app .controller('multipleFlightsController', function ($scope, $http, $location) { $scope.addPassenger = function () { document.getElementById('forPassengers') .innerHTML += '<input class="form-control airport ng-pristine ng-untouched ng-valid" type="text" ng-model="additionalPassengers" placeholder="#" tabindex="0" aria-invalid="false"> '; }; /*..... more code*/

WORKING SOLUTION (edit)

Eventually what solved my problem was actually using AngularJS instead of accessing the input element through the document.getElementById function.

最满意答案

使用您的$ scope:

$scope.passengers = []; $scope.addPassenger = function () { $scope.passengers.push({}); };

然后使用ng-repeat:

<!-- use ng-repeat... --> <input ng-repeat="passenger in passengers" class="form-control airport ng-pristine ng-untouched ng-valid" type="text" ng-model="additionalPassengers" placeholder="#" tabindex="0" aria-invalid="false">

Use your $scope:

$scope.passengers = []; $scope.addPassenger = function () { $scope.passengers.push({}); };

and then with ng-repeat:

<!-- use ng-repeat... --> <input ng-repeat="passenger in passengers" class="form-control airport ng-pristine ng-untouched ng-valid" type="text" ng-model="additionalPassengers" placeholder="#" tabindex="0" aria-invalid="false">

更多推荐

本文发布于:2023-07-28 06:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1301636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   Button   click   works

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!