I have 2 drop downs and I can move the objects from one to another using the buttons. I want to get the updated values after I am done with moving them from one drop down to another. the values in the array is coming out to be null.
我有2个下拉,我可以使用按钮将对象从一个移动到另一个。我希望在完成将它们从一个下拉移动到另一个下拉后获取更新的值。数组中的值将为null。
How can I get the updated value of all the options in an array. I need to pass the updated values to the controller from one function like function AddFiles(iniIdArray,afterIdArray)
如何获取阵列中所有选项的更新值。我需要从一个函数将更新的值传递给控制器,如函数AddFiles(iniIdArray,afterIdArray)
http://jsfiddle.net/678hmujh/1/
var varInitialId = document.getElementById('iniId');
for (i = 0; i < varInitialId.options.length; i++) {
iniIdArray[i] = varInitialId.options[i].value;
}
var varAfterId = document.getElementById('afterId');
for (i = 0; i < varAfterId.options.length; i++) {
afterIdArray[i] = varAfterId.options[i].value;
}
2 个解决方案
#1
You could use map: http://jsfiddle.net/678hmujh/4/
你可以使用地图:http://jsfiddle.net/678hmujh/4/
$('#listButton').on('click', function() {
var first = $('#iniId option').map(function(){ return this.value; });
var second = $('#afterId option').map(function(){ return this.value; });
console.log( first.toArray() );
console.log( second.toArray() );
});
By doing first.toArray() or second.toArray() you can get a basic javascript array of what was built.
通过执行first.toArray()或second.toArray(),您可以获得构建内容的基本javascript数组。
#2
$('select#iniId > option').each(function() {
$(this).text(); //is your option text
$(this).val(); //is your option value
});
#1
You could use map: http://jsfiddle.net/678hmujh/4/
你可以使用地图:http://jsfiddle.net/678hmujh/4/
$('#listButton').on('click', function() {
var first = $('#iniId option').map(function(){ return this.value; });
var second = $('#afterId option').map(function(){ return this.value; });
console.log( first.toArray() );
console.log( second.toArray() );
});
By doing first.toArray() or second.toArray() you can get a basic javascript array of what was built.
通过执行first.toArray()或second.toArray(),您可以获得构建内容的基本javascript数组。
#2
$('select#iniId > option').each(function() {
$(this).text(); //is your option text
$(this).val(); //is your option value
});