我还是 html javascript 的新手。我想问我可以使用 for 循环来优化或使它动态
var port = [];
port[0]=$('#slcPort_0').val();
port[1]=$('#slcPort_1').val();
port[2]=$('#slcPort_2').val();
port[3]=$('#slcPort_3').val();
port[4]=$('#slcPort_4').val();
我在函数中使用这段代码从 html 表单中检索数据
谢谢
最佳答案
你可以使用:
// selects all the elements whose 'id' starts-with "slcPort_":
var port = $('[id^=slcPort_]').map(function(){
// returns the value from those elements:
return this.value;
// converts to an array:
}).get();
这不能保证按数字顺序排列,但会按照这些元素在 DOM 中出现的顺序排列。
引用资料:
关于javascript - 如何优化或使其动态化(html javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27141217/