Javascript 获取非计数id的值 /*使用jquery获取非计数列表的值*/ 功能保存(id){ var x=“loc-”+id; $('input[id^=“+x+”])。每个(函数(){ 警报(this.value);//$(this.val(); }); }
我需要将变量x放入循环中,但它不起作用您需要正确连接您的值:Javascript 获取非计数id的值 /*使用jquery获取非计数列表的值*/ 功能保存(id){ var x=“loc-”+id; $('input[id^=“+x+”])。每个(函数(){ 警报(this.value);//$(this.val(); }); },javascript,jquery,Javascript,Jquery,我需要将变量x放入循环中,但它不起作用您需要正确连接您的值: /*get value of non count list,using jquery */ <input type="text" id="loc-51-0" value="ahmed"> <input type="text" id="loc-51-0" value="ahmed"> <input type="text" id="loc-51-1" value="mohamed"> <butt
/*get value of non count list,using jquery */
<input type="text" id="loc-51-0" value="ahmed">
<input type="text" id="loc-51-0" value="ahmed">
<input type="text" id="loc-51-1" value="mohamed">
<button onclick="save(51)">
<input type="text" id="loc-52-0" value="alaa">
<input type="text" id="loc-52-1" value="karim">
<button onclick="save(52)">
function save(id){
var x="loc-"+id;
$('input[id^="+x+"]').each(function() {
alert( this.value ); // $(this).val();
});
}
另外,由于id
是唯一的,您只需要[id^=
而不是input[id^=
尝试以下方法:
$('[id^="'+ x +'"]').each(function() {
alert(this.value);
});
$('input[id^=“+x+”)。每个(函数(){
警惕(this.valule);
})您遇到语法错误,请更改以下内容:
$('input[id^="'+ x +'"]').each(function() {
alert( this.valule );
}) <- you're missing this `)`
为此:
$('input[id^="+x+"]').each(function() {
alert( this.value ); // $(this).val();
});
$('input[id^="'+x+'"]').each(function() {
alert( this.value ); // $(this).val();
});