Javascript jQuery获取输入:第n个子(n)值不工作
我想说的是,当型号改变时,价格的价值也会改变。要想得到价格,我需要身份证号码和数量 所以我得到了这个HTML代码:Javascript jQuery获取输入:第n个子(n)值不工作,javascript,jquery,html,css-selectors,Javascript,Jquery,Html,Css Selectors,我想说的是,当型号改变时,价格的价值也会改变。要想得到价格,我需要身份证号码和数量 所以我得到了这个HTML代码: <div class="col-sm-9 topnav"> <span> <input name="prodid[]" type="text" id="form-field-icon-1" class="i
<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>
我得到的数量值未定义,但当我将第n个子项更改为1时,我会得到我输入的数量值。在my console.log中,有一个值。
上下文:输入#form-field-icon-2.input-small
非常感谢你的帮助 因为
输入是其父span
的第一个子项
你需要
var qty = thisObj.closest('div').children(':nth-child(3)').children("input").val();
或者,最简单的方法是通过名称查找输入
var qty = thisObj.closest('div').find('input[name="qty[]"]').val();
解决了
var test = thisObj.closest('div').children().children("input:nth-child(1)");
var model = $(test[1]).val();
我能得到我需要的东西。:) 您提供的标记中没有索引为2的
如果您想获得第二个内部div class=“col-sm-9 topnav”,只需执行以下操作:
var second = $("div.topnav input")[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();
}
:n输入字段中的第n个子项(3)工作不正常。记住一件事:eq数组是从0索引开始的
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();
}
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();
}