从文本框中获取值
Getting the value from a textbox
我对使用 jquery 从文本框中获取值有疑问。我的代码如下:
<table width="500" border="0">
<tr>
<td><input type="text" class="quantity" /></td>
<td><input type="button" class="btn" p_id="item2" value="submit" /></td>
</tr>
<tr>
<td><input type="text" class="quantity" /></td>
<td><input type="button" class="btn" p_id="item3" value="submit" /></td>
</tr>
<tr>
<td><input type="text" class="quantity" /></td>
<td><input type="button" class="btn" p_id="item4" value="submit" /></td>
</tr>
</table>
<script>
$(document).ready(function(){
$('.btn').click(function(){
var p_id = $(this).attr("p_id");
var q_ty = $('#quantity').val();
$.post("updateQty.php",{p_id:p_id, q_ty:q_ty},function(result){
alert(result);
});
});
});
</script>
我已设法获取项目的 ID,但无法获取所选文本框的值。
由于您所有的文本框都具有相同的 ID,因此您将无法 select 任何特定文本框的值。
相反,您可以根据单击的按钮获取最近的 <tr>
元素,然后找到输入并以此方式获取值。
例如
<table width="500" border="0">
<tr>
<td><input type="text" class="quantity-input" /></td>
<td><input type="button" class="btn" p_id="item2" value="submit" /></td>
</tr>
<tr>
<td><input type="text" class="quantity-input"/></td>
<td><input type="button" class="btn" p_id="item3" value="submit" /></td>
</tr>
<tr>
<td><input type="text" class="quantity-input" /></td>
<td><input type="button" class="btn" p_id="item4" value="submit" /></td>
</tr>
</table>
<script>
$(document).ready(function(){
$('.btn').click(function(){
var row = $(this).closest('tr');
var p_id = $(this).attr("p_id");
var q_ty = row.find(".quantity-input").val();
$.post("updateQty.php",{p_id:p_id, q_ty:q_ty},function(result){
alert(result);
});
});
});
</script>
注意,每个输入都有一个class到select,而不是id。
有几个 id = quantity 的输入...我认为你应该改变它们,这样你就可以通过连接你找到的 p_id 来区分
<table width="500" border="0">
<tr>
<td><input type="text" id="quantity_item2" /></td>
<td><input type="button" class="btn" p_id="item2" value="submit" /></td>
</tr>
<tr>
<td><input type="text" id="quantity_item3" /></td>
<td><input type="button" class="btn" p_id="item3" value="submit" /></td>
</tr>
<tr>
<td><input type="text" id="quantity_item4" /></td>
<td><input type="button" class="btn" p_id="item4" value="submit" /></td>
</tr>
</table>
<script>
$(document).ready(function(){
$('.btn').click(function(){
var p_id = $(this).attr("p_id");
var q_ty = $('#quantity_' + p_id).val();
$.post("updateQty.php",{p_id:p_id, q_ty:q_ty},function(result){
alert(result);
});
});
});
</script>
我对使用 jquery 从文本框中获取值有疑问。我的代码如下:
<table width="500" border="0">
<tr>
<td><input type="text" class="quantity" /></td>
<td><input type="button" class="btn" p_id="item2" value="submit" /></td>
</tr>
<tr>
<td><input type="text" class="quantity" /></td>
<td><input type="button" class="btn" p_id="item3" value="submit" /></td>
</tr>
<tr>
<td><input type="text" class="quantity" /></td>
<td><input type="button" class="btn" p_id="item4" value="submit" /></td>
</tr>
</table>
<script>
$(document).ready(function(){
$('.btn').click(function(){
var p_id = $(this).attr("p_id");
var q_ty = $('#quantity').val();
$.post("updateQty.php",{p_id:p_id, q_ty:q_ty},function(result){
alert(result);
});
});
});
</script>
我已设法获取项目的 ID,但无法获取所选文本框的值。
由于您所有的文本框都具有相同的 ID,因此您将无法 select 任何特定文本框的值。
相反,您可以根据单击的按钮获取最近的 <tr>
元素,然后找到输入并以此方式获取值。
例如
<table width="500" border="0">
<tr>
<td><input type="text" class="quantity-input" /></td>
<td><input type="button" class="btn" p_id="item2" value="submit" /></td>
</tr>
<tr>
<td><input type="text" class="quantity-input"/></td>
<td><input type="button" class="btn" p_id="item3" value="submit" /></td>
</tr>
<tr>
<td><input type="text" class="quantity-input" /></td>
<td><input type="button" class="btn" p_id="item4" value="submit" /></td>
</tr>
</table>
<script>
$(document).ready(function(){
$('.btn').click(function(){
var row = $(this).closest('tr');
var p_id = $(this).attr("p_id");
var q_ty = row.find(".quantity-input").val();
$.post("updateQty.php",{p_id:p_id, q_ty:q_ty},function(result){
alert(result);
});
});
});
</script>
注意,每个输入都有一个class到select,而不是id。
有几个 id = quantity 的输入...我认为你应该改变它们,这样你就可以通过连接你找到的 p_id 来区分
<table width="500" border="0">
<tr>
<td><input type="text" id="quantity_item2" /></td>
<td><input type="button" class="btn" p_id="item2" value="submit" /></td>
</tr>
<tr>
<td><input type="text" id="quantity_item3" /></td>
<td><input type="button" class="btn" p_id="item3" value="submit" /></td>
</tr>
<tr>
<td><input type="text" id="quantity_item4" /></td>
<td><input type="button" class="btn" p_id="item4" value="submit" /></td>
</tr>
</table>
<script>
$(document).ready(function(){
$('.btn').click(function(){
var p_id = $(this).attr("p_id");
var q_ty = $('#quantity_' + p_id).val();
$.post("updateQty.php",{p_id:p_id, q_ty:q_ty},function(result){
alert(result);
});
});
});
</script>