我如何知道我应该从哪个隐藏字段中获取一个值,因为单击了一个与隐藏字段相邻的按钮。这个按钮也在while循环中,因此产生了一个按钮和隐藏字段的列表。每个按钮旁边都有一个隐藏字段。
我想使用jquery ajax将隐藏字段中的数据插入到数据库中的表中,但是我循环了所有的按钮来找出哪一个被点击了,它工作得很好,但问题出现在wen中,它循环遍历隐藏字段,它获取最后一个隐藏字段的值。如何获得与所单击按钮相邻的隐藏字段的值?我会非常感谢你的帮助。提前谢谢。
<Table id="list_tb">
<?php
while($rows=mysql_fetch_assoc(orderSQL))
{
?>
<tr>
<td><?php echo $rows['name'];?></td>
<td>
<form>
<input type="submit" name="add_btn" id="add_btn" value="Add"/>
<input type="hidden" name="order_id" id="order_id" value="<?php echo $rows['order_id'];?>"/>
</form
</td>
</tr>
<?php
}
?>
</Table>JQUERY
$(#list_tb input[type=submit]).each(function(){
$(this).click(function(){
$(#list_tb input[type=hidden]).each(function(){
var value=$(this).val();
alert(value);
});
});
});发布于 2011-01-25 16:30:16
像这样说你的html
<div id="parent">
<div class="child">
<span><input type="hidden" class="hid" value="1"></span>
<span><input type="button" class="but" value="submit"></span>
</div>
<div class="child">
<span><input type="hidden" class="hid" value="3"></span>
<span><input type="button" class="but" value="submit"></span>
</div>
</div>Jquery代码如下
jQuery(".but").click(function(){
var hidVal = jQuery(this).closest("div.child").find("hid").val();
});发布于 2011-01-25 16:37:18
我只是选择了Harish....you提供的html,也可以这样做..
<div id="parent">
<div class="child">
<span><input type="hidden" class="hid" value="1"></span>
<span><input type="button" class="but" value="submit"></span>
</div>
<div class="child">
<span><input type="hidden" class="hid" value="3"></span>
<span><input type="button" class="but" value="submit"></span>
</div>
而jquery将是..
jQuery(".but").click(function(){
var hidVal = jQuery(this).parents("div.child").find("hid").val();
});发布于 2011-01-25 22:25:40
感谢各位,我解决了它,这是一个很长的短文,但它很有效,对于任何需要这段代码的人来说,它是这样进行的
<form id="form1" name="form1" method="post" action="load_modules.php">
<table width="500" id="orderlist_table">
<tr>
<th width="78" style="font-size: 12px">type</th>
<th width="82" style="font-size: 12px">amount</th>
<th width="90" style="font-size: 12px">date</th>
<th colspan="5" style="font-size: 12px">invoice order</th>
</tr>
<?php
while($order_rows=mysql_fetch_assoc($orderSQL))
{
?>
<tr>
<td height="26" style="font-size: 12px"><?php echo $order_rows['Type']; ?></td>
<td style="font-size: 12px"><?php echo $order_rows['Total_amount']; ?></td>
<td style="font-size: 12px"><?php echo $order_rows['Date']; ?></td>
<td width="55" id="invoice_btn_td" style="font-size: 12px">
<input type="submit" name="invoice_btn" id="hf<?php echo $order_rows['id'];?>" value="invoice"/>
</td>
<td width="30" id="userid_td" style="font-size: 12px">
<input type="hidden" name="user_idhf" id="hf<?php echo $order_rows['id'];?>" value="<?php echo $order_rows['User_id']; ?>" />
</td>
<td width="35" id="type_td" style="font-size: 12px" >
<input type="hidden" name="typehf" id="hf<?php echo $order_rows['id'];?>" value="<?php echo $order_rows['Type'];?>" />
</td>
<td width="37" id="order_td" style="font-size: 12px">
<input type="hidden" name="orderidhf" id="hf<?php echo $order_rows['id'];?>" value="<?php echo $order_rows['id'];?>" />
</td>
<td width="57" id="amount_td" style="font-size: 12px">
<input type="hidden" name="total_amounthf" id="hf<?php echo $order_rows['id'];?>" value="<?php echo $order_rows['Total_amount']?>" />
</td>
</tr>
<?php
}
?>
</table>jQuery:
$("#orderlist_table input[type=submit]").each(function(){
$(this).click(function(){
var btn_id=$(this).attr('id');
var userid_value;
var type_value;
var orderid_value;
var totalamount_value;
$("#userid_td input[type=hidden]").each(function(){
var userid_id=$(this).attr('id');
if(userid_id==btn_id)
{
userid_value=$(this).attr('value');
}
});
$("#type_td input[type=hidden]").each(function(){
var type_id=$(this).attr('id');
if(type_id==btn_id)
{
type_value=$(this).attr('value');
}
});
$("#order_td input[type=hidden]").each(function(){
var order_id=$(this).attr('id');
if(order_id==btn_id)
{
orderid_value=$(this).attr('value');
}
});
$("#amount_td input[type=hidden]").each(function(){
var amount_id=$(this).attr('id');
if(amount_id==btn_id)
{
totalamount_value=$(this).attr('value');
}
});
//alert(userid+" "+type_value+" "+orderid_value+" "+totalamount_value);
$.post("php_files/add_invoice.php",{user_idhf:userid_value,
typehf:type_value,
orderidhf:orderid_value,
total_amounthf:totalamount_value},
function(data)
{
if(data.success)
{
alert(data.success);
}
if(!data.success)
{
alert(!data.success);
}
},'json');
return false;
});
});只需将按钮命名为与隐藏字段相同的名称,并在末尾加上计数器编号以区分它们,然后将单击按钮的名称等同于隐藏字段,如果它们相等,则它们在同一行上,或者更确切地说,它们彼此相邻。就是这样。
谢谢伙计们
https://stackoverflow.com/questions/4791243
复制相似问题