Javascript 为什么它只返回数组的第一个元素,而i';你已经打印了数组的所有值了吗?

Javascript 为什么它只返回数组的第一个元素,而i';你已经打印了数组的所有值了吗?,javascript,jquery,html,Javascript,Jquery,Html,hy 我想在html标记中获取值,但它只返回第一个值,我不知道为什么。。。(我想要类为“iHidden”的html标记的值) (我完全能够用javascript中的name=“service[]”获取第一个输入的值) while($service=$req->fetch()){?> € 以下是我的javascript: for(var i = 0; i <= totalService; i++){ if($('#'+i).is(":checked")){ /

hy

我想在html标记中获取值,但它只返回第一个值,我不知道为什么。。。(我想要类为“iHidden”的html标记的值)

(我完全能够用javascript中的name=“service[]”获取第一个输入的值)

while($service=$req->fetch()){?>

以下是我的javascript:

for(var i = 0; i <= totalService; i++){

    if($('#'+i).is(":checked")){

        // For the first input (it works)

        var total = $('#'+i).val();

        globalTotal = parseInt(globalTotal )+ parseInt(total); 

        // End for the first input



        // Here is the problem : it returns only the first value in my database...
        getI[i] = $('.iHidden').html();

        console.log(getI); // shows the first value


    }
}
for(var i=0;i该方法返回集合中第一个元素的HTML内容。在您的示例中,
.iHidden
选择类为
iHidden
的所有元素

获取匹配元素集中第一个元素的HTML内容,或者设置每个匹配元素的HTML内容

要解决此问题,请根据total元素的父元素选择。其中方法可用于获取公共祖先,并使用方法获取
iHidden
元素


供参考:

注意:使用ASCII字母、数字、“"”、“-”和“.”以外的字符可能会导致兼容性问题,因为它们在HTML 4中是不允许的。虽然HTML 5中取消了此限制,但为了兼容性,ID应以字母开头

因此,最好以字母而不是数字开始id值

例如:

PHP

该方法返回集合中第一个元素的HTML内容。在您的示例中,
.iHidden
选择类为
iHidden
的所有元素

获取匹配元素集中第一个元素的HTML内容,或者设置每个匹配元素的HTML内容

要解决此问题,请根据total元素的父元素选择。其中方法可用于获取公共祖先,并使用方法获取
iHidden
元素


供参考:

注意:使用ASCII字母、数字、“"”、“-”和“.”以外的字符可能会导致兼容性问题,因为它们在HTML 4中是不允许的。虽然HTML 5中取消了此限制,但为了兼容性,ID应以字母开头

因此,最好以字母而不是数字开始id值

例如:

PHP


这就是
html()
函数工作的方式这里有一个更好的想法的图像:“获取匹配元素集中第一个元素的html内容或设置每个匹配元素的html内容。这就是
html()
函数工作的方式这里有一个更好的想法的图像:”获取匹配元素集中第一个元素的HTML内容,或设置每个匹配元素的HTML内容。@pandaD:很高兴听到:)@pandaD:很高兴听到:)
for(var i = 0; i <= totalService; i++){

    if($('#'+i).is(":checked")){

        // For the first input (it works)

        var total = $('#'+i).val();

        globalTotal = parseInt(globalTotal )+ parseInt(total); 

        // End for the first input



        // Here is the problem : it returns only the first value in my database...
        getI[i] = $('.iHidden').html();

        console.log(getI); // shows the first value


    }
}
getI[i] = $('#'+i).closest('.form-control').find('.iHidden').html();
<input type="checkbox" name="service[]" value="<?= $service['price']; ?>" id="total<?= $i++ ?>">
for (var i = 0; i <= totalService; i++) {
  var $tot = $('#total' + i);
  if ($tot.is(":checked")) {
    var total = $tot.val();
    globalTotal = parseInt(globalTotal) + parseInt(total);
    getI[i] = $tot.closest('.form-control').find('.iHidden').html();
    console.log(getI);
  }
}