2016-12-28 316 views
1

我想,当我点击文本字段输入文本的价值,它的值由2具有相同名称的更改Angular.js

<input type='text' name='field[]' ng-model='field' ng-click='changeval()'> 
<input type='text' name='field[]' ng-model='field' ng-click='changeval()'> 
<input type='text' name='field[]' ng-model='field' ng-click='changeval()'> 
<input type='text' name='field[]' ng-model='field' ng-click='changeval()'> 

$scope.changeval=function(){ 
    //set value of input text for 2  
} 

http://fiddle.jshell.net/0w36h8zm/

+0

为什么你需要这样做吗?为什么输入必须是相同的名称? – Daniel

+0

是一个例子,是我需要的任何 – yavg

回答

0

如果你想改变要改变该值中的所有文本字段做到这一点:

angular.module("myApp", ['ui.bootstrap']) 
.controller("MyCtrl", function($scope, $modal) { 
    $scope.changeval=function(){ 
    $scope.field=2; 
} 

}); 

如果你想改变文本字段的值,在其上单击,然后做到这一点:

 <input type='text' name='field[]' ng-model='field1' ng-click='changeval1()'> 
     <input type='text' name='field[]' ng-model='field2' ng-click='changeval2()'> 
      <input type='text' name='field[]' ng-model='field3' ng-click='changeval3()'> 
       <input type='text' name='field[]' ng-model='field4' ng-click='changeval4()'> 




    angular.module("myApp", ['ui.bootstrap']) 
.controller("MyCtrl", function($scope, $modal) { 
$scope.changeval1=function(){ 
$scope.field1=2; 
} 

$scope.changeval2=function(){ 
$scope.field2=2; 
} 

$scope.changeval3=function(){ 
$scope.field3=2; 
} 


$scope.changeval4=function(){ 
$scope.field4=2; 
} 
}); 

http://fiddle.jshell.net/0w36h8zm/3/

+0

谢谢,但我需要为输入具有相同的名称....我可以有n个字段.. – yavg

+0

它们都具有相同的名称='field []'。只有模型和功能发生了变化。 – Adds

1

使用数组和索引作为参数,如果你有很多领域

<input type='text' name='field[]' ng-model='field[0]' ng-click='changeval(0)'> 
<input type='text' name='field[]' ng-model='field[1]' ng-click='changeval(1)'> 
<input type='text' name='field[]' ng-model='field[2]' ng-click='changeval(2)'> 
<input type='text' name='field[]' ng-model='field[3]' ng-click='changeval(3)'> 

JS

angular.module("myApp", ['ui.bootstrap']) 
.controller("MyCtrl", function($scope, $modal) { 

    $scope.field = []; 
     $scope.changeval=function(index){ 
     //change the val for 2; 
     $scope.field[index]=2; 
    }; 


}); 

http://fiddle.jshell.net/0w36h8zm/9/