Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取非计数id的值 /*使用jquery获取非计数列表的值*/ 功能保存(id){ var x=“loc-”+id; $('input[id^=“+x+”])。每个(函数(){ 警报(this.value);//$(this.val(); }); }_Javascript_Jquery - Fatal编程技术网

Javascript 获取非计数id的值 /*使用jquery获取非计数列表的值*/ 功能保存(id){ var x=“loc-”+id; $('input[id^=“+x+”])。每个(函数(){ 警报(this.value);//$(this.val(); }); }

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

我需要将变量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">
<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();
});