gpt4 book ai didi

javascript - 在第二次选择时更改值并保留值

转载 作者:行者123 更新时间:2023-11-28 04:20:54 24 4
gpt4 key购买 nike

您好,当我单击第一个选择器的值时,我想从 mysql 加载第二个选择的数据选项。并保留最后选择的字段的JSON数据并保留追加功能。

我知道如何从 mysql 导入数据,但我想在每次更改第一个选择器时获取值,而不更改所有第二个选择器,仅更改相关选择器。

它是如何工作的。我选择一个类别(代码中未显示)最重要的是:select 1 接收所选类别的所有类别。select 2 接收 select 1 子类别的所有类别

当第一个类别更改时,会加载第二个选择器,但不要忘记我需要添加无限的字段而不更改所有第二个选择器

我使用此代码: https://plnkr.co/edit/kKjhpy0fdnOprFA8PAYx?p=preview

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="appCtrl">
<h3>Add Input fields dynamically</h3>



<button ng-click="addSelectItem()">Add select Field</button>

<div ng-repeat="item in selects">
<select ng-model="item.type"><option ng-repeat='option1 in type' value='{{option1.nomcarac}}' >{{option1.nomcarac}}</option></select>
<select ng-model="item.brandname" ><option ng-repeat='option1 in valuelist' value='{{option1.nomcarac}}' >{{option1.nomcarac}}</option></select>
<button ng-click="getValue(item)">get input value</button>
</div>



<pre>{{inputs | json}}</pre>
<pre>{{selects | json}}</pre>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('appCtrl', function($scope) {
$scope.inputs = [];
$scope.selects = [];
$scope.type = [{nomcarac: "phone"},{nomcarac: "shoes"}]
$scope.phonelist = [{nomcarac: "apple"},{nomcarac: "motorola 10g"}]
$scope.valuelist = [{nomcarac: "test"},{nomcarac: "test2"}]
$scope.shoeslist = [{nomcarac: "jean michel"},{nomcarac: "motorola 10g"}]


var count = 1;
var fieldname;
$scope.addItem = function(){
fieldname = "Field " + count;
$scope.inputs.push({name: fieldname});
count++;
}

$scope.addSelectItem = function(){
fieldname = "Field " + count;
$scope.selects.push({name: fieldname});
count++;
}
$scope.getValue = function(item){
alert(item.value);
}

});
</script>

</body>
</html>

最佳答案

如果我理解正确的话,每次更改组中第一个选择的值时,您都希望填充第二个选择的选项。如果是这样,您可以使用ngChange完成此操作并将值列表存储在相应的 item 对象中:

var app = angular.module('myApp', []);
app.controller('appCtrl', function($scope) {

$scope.inputs = [];
$scope.selects = [];

$scope.type = [{nomcarac: "phone"}, {nomcarac: "shoes"}];

$scope.phonelist = [{nomcarac: "apple"}, {nomcarac: "motorola 10g"}];
$scope.shoeslist = [{nomcarac: "jean michel"}, {nomcarac: "gucci"}];

var count = 1;
var fieldname;
$scope.addItem = function(){
fieldname = "Field " + count;
$scope.inputs.push({name: fieldname});
count++;
};

$scope.addSelectItem = function(){
fieldname = "Field " + count;
$scope.selects.push({name: fieldname});
count++;
};

$scope.getValue = function(item){
console.log(item);
};

$scope.getValuesList = function(item) {
//get actual data from db
switch (item.type){
case 'phone':
item.valuelist = angular.copy($scope.phonelist);
break;
case 'shoes':
item.valuelist = angular.copy($scope.shoeslist);
break;
default:
item.valuelist = [];
break;
}
};

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div ng-app="myApp" ng-controller="appCtrl">
<h3>Add Input fields dynamically</h3>

<button ng-click="addSelectItem()">Add select Field</button>

<div ng-repeat="item in selects">

<select ng-model="item.type" ng-change='getValuesList(item)'>
<option ng-repeat='option in type' value='{{::option.nomcarac}}' >{{::option.nomcarac}}</option>
</select>

<select ng-model="item.brandname">
<option ng-repeat='option in item.valuelist' value='{{::option.nomcarac}}' >{{::option.nomcarac}}</option>
</select>

<button ng-click="getValue(item)">get input value</button>
</div>

<pre>{{inputs | json}}</pre>
<pre>{{selects | json}}</pre>
</div>

关于javascript - 在第二次选择时更改值并保留值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45444730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com