2016-12-28 546 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/