2014-11-03 33 views
0

我想要的是当型号改变时,价格的价值会改变。为了得到价格,我需要ID号码和数量。jQuery获取输入:nth-​​child(n)的值不起作用

所以我得到这个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(); 
} 

数量的值,我得到的是不确定的,但是当我改变了第n个孩子到1,我得到我输入的价值。在我的console.log中,有一个值。 背景:输入#form-field-icon-2.input-small

非常感谢您的帮助!

回答

2

因为input是其母公司span的第一个孩子。

你需要

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

或最简单的方法是通过它的名字找到输入

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

我会尝试,后来非常感谢! :) – bless1204 2014-11-03 05:40:22

0

解决它!

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

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

0

没有<input>索引2在你提供的标记。

如果你想获得第二个div类= “COL-SM-9 topnav” 里面那么简单的做到这些:

var second = $("div.topnav input")[1]; 
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(); 
} 

试试这个