【问题标题】:jQuery getting input:nth-child(n) value not workingjQuery获取输入:nth-​​child(n)值不起作用
【发布时间】:2014-12-29 18:17:44
【问题描述】:

我想要的是,当型号改变时,价格的价值也会改变。而要获得价格,我需要身份证号和数量。

所以我得到了这个 HTML 代码:

<div class="col-sm-9 topnav">
    <span>                                                              
        <input name="prodid[]" type="text" id="form-field-icon-1" class="input-small" placeholder="Product ID" />
    </span>
    <span>
        <input name="model[]" type="text" id="form-field-icon-2" class="input-small" onchange="getprice($(this));"placeholder="Model" />
    </span>
    <span>
        <input name="qty[]" type="text" id="form-field-icon-3" class="input-small" placeholder="Quantity" />
    </span>
        <input name="price[]" type="text" id="form-field-icon-2" class="input-small" placeholder="Price" readonly="readonly" />                                                             
    <span>
        <button class="btn btn-minier btn-yellow">Add</button>
    </span>
</div>

JS代码:

function getprice(thisObj) {
    var prodid = thisObj.closest('div').children().children("input:first").val();
    var modelid = thisObj.val();
    var qty = thisObj.closest('div').children().children("input:nth-child(2)").val();
}

我得到的 qty 的值是未定义的,但是当我将第 n 个孩子更改为 1 时,我得到了我输入的值。在我的 console.log 中,有一个值。 上下文:输入#form-field-icon-2.input-small

非常感谢您的帮助!

【问题讨论】:

    标签: javascript jquery html css-selectors


    【解决方案1】:

    你应该像这样改变你的javascript代码

    function getprice(thisObj) {
        var prodid = thisObj.closest('div').children().children("input:eq(0)").val();
        var modelid = thisObj.val();
        var qty = thisObj.closest('div').children().children("input:eq(1)").val();
    }
    

    :nth-child(3) 在输入字段中无法正常工作。记住一件事:eq 数组是从 0 索引开始的。

    【讨论】:

      【解决方案2】:
      function getprice(thisObj) {
          var prodid = thisObj.closest('div').children().children("input:first").val();
          var modelid = thisObj.val();
          var qty = thisObj.closest('div').children().children("input:eq(1)").val();
      }
      

      试试这个

      【讨论】:

        【解决方案3】:

        您提供的标记中没有索引为 2 的 &lt;input&gt;

        如果你想在 div class="col-sm-9 topnav" 中获得第二名,那么只需这样做:

        var second = $("div.topnav input")[1];
        

        【讨论】:

          【解决方案4】:

          解决了!

          var test = thisObj.closest('div').children().children("input:nth-child(1)");
          var model = $(test[1]).val();
          

          我能够得到我需要的东西。 :)

          【讨论】:

            【解决方案5】:

            因为input 是其父span 的第一个孩子。

            你需要

            var qty = thisObj.closest('div').children(':nth-child(3)').children("input").val();
            

            或者最简单的方法是通过名称查找输入

            var qty = thisObj.closest('div').find('input[name="qty[]"]').val();
            

            【讨论】:

              猜你喜欢
              • 2017-06-09
              • 1970-01-01
              • 1970-01-01
              • 2014-06-07
              • 2023-03-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-14
              相关资源
              最近更新 更多