2011-01-25 57 views
0

如何知道哪個隱藏字段可以從給定的按鈕旁邊的隱藏字段獲取值。這個按鈕也在while循環中,因此產生了一個按鈕和隱藏字段的列表。每個按鈕都有一個與其相鄰的隱藏字段。循環顯示錶格行中的php while循環中的隱藏字段

我想要使用jquery ajax將數據從隱藏字段插入到數據庫中的表中,但是我通過所有按鈕循環查找哪個被單擊,它工作正常,但問題出現在我的循環中通過隱藏字段,它獲得最後一個隱藏字段的值。我如何獲得與點擊按鈕相鄰的隱藏字段的值? 我真的很感謝幫助。提前Thanx。

jQuery的

$(#list_tb input[type=submit]).each(function(){ 
$(this).click(function(){ 
$(#list_tb input[type=hidden]).each(function(){ 
var value=$(this).val(); 
alert(value); 
}); 
}); 
}); 

回答

0

說你的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(); 
}); 
0

我只是通過採摘哈里什給出的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(); 
}); 
0

感謝名單guyz,我得到了解決這個問題,它的一較長短,但它的工作,任何人誰需要這個代碼,這是它是如何去

<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; 
    }); 


}); 

只需在按鈕上使用與隱藏字段相同的名稱並在末尾添加一個計數器號碼即可區分它們,然後將點擊的按鈕的名稱等同於隱藏字段,如果它們相同,則它們位於同一行或而是彼此相鄰。而已。

Thanx guyz