我想通过点击复选框得到文本框输入的值,下面的选择器错在哪里的啊??
<form action="doShopping.jsp" id="order_shopping" name="order_shopping" onsubmit="return checkShopping();">
<table id="table" class="fl">
<tbody>
<tr>
<th>商品名</th>
<th>单价</th>
<th>购买数量</th>
<th><input id="both" type="checkbox" name="both" autocomplete="off"></th>
</tr>
<tr>
<td>香蕉</td>
<td>100</td>
<td>
<input type="text" name="count" placeholder="请输入数量" autocomplete="off"/>
</td>
<td>
<input type="checkbox" name="choice" autocomplete="off">
</td>
</tr>
</tbody>
</table>
<input type="submit" id="add_shopping" value="添加购物车"/>
</form>
<script type="text/javascript">
$(function(){
var $current_count = $("#order_shopping #table tr td> input[name='choice']:checked").parent().prev().find("input[name='count']").val();
console.log($current_count);
})
</script>
问题是你的输入框name="both"不是count
选择器没错,不过你要点击获取应该绑定click事件,而不是dom ready中获取,由于你这个html代码choice没用选中,多以没选出对象,所以log不出内容
$("#order_shopping #table tr td> input[name='choice']").click(function(){
if(!this.checked)return;//未选中状态
var $current_count=$(this).parent().prev().find("input[name='count']").val();
alert($current_count);
});