简体   繁体   English

识别jQuery按钮单击PHP行

[英]identify jquery button click in php rows

I have an input button in : 我在中有一个输入按钮:

while ($row = $result->fetch()) {
echo '<table class="cart-row" cellspacing="0" cellpadding="0" width="100%">';
echo '<tbody>';
echo '<tr>';
echo '<td width="75"><img border="0" width="59px" height="78px" title="" alt="" src=' . $row["ImagePath"] .'></td>';
echo '<td width="203"><span id="itemName" class="itemElements">'. $row["Name"] .'</span></td>';
echo '<td width="135"><span id="qtyNum">('. $row["Qty"] .')</span> <br />';
echo '<span id="qtyRemoveLink"><input class="linkbtn" type="submit" id="btnRemove" value="Remove"></td>';                            
echo '<td width="180"><span id="orderStatus" class="itemElements">In Stock Usually dispatched within 24 hours</span></td>';
echo '<td width="175" id="itemPriceRow"><span id="itemPrice">€ '. $row["Price"] .'</span></td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '<br>';                            
}  

I'm trying to use this method to trigger an event when the button is clicked however the event is only being fired for the first button generated in the first row. 我试图使用此方法在单击按钮时触发事件,但是仅针对第一行中生成的第一个按钮触发该事件。 I'm using this method: 我正在使用这种方法:

$(document).ready(function() {
                $('#btnRemove').click(function() {
                     alert("test");
                });
             });

Any ideas how I can fix this problem? 有什么想法可以解决这个问题吗? I know in C# there is on row databound method however in jQuery I dont now if it exsists. 我知道在C#中有行数据绑定方法,但是在jQuery中,如果存在,我现在不知道。 Thanks 谢谢

You are selecting an element to bind the click event handler to that has a unique ID. 您正在选择将click事件处理程序绑定到具有唯一ID的元素。 When you select by ID you will be returned only the first matching element because to be a valid ID it must be unique. 当您按ID选择时,将仅返回第一个匹配元素,因为要成为有效ID,它必须是唯一的。

Change the ID to a class and change your selector to .btnRemove and the alert will work for all the buttons. 将ID更改为类,并将选择器更改为.btnRemovealert将对所有按钮起作用。

echo '<span id="qtyRemoveLink"><input class="linkbtn" type="submit" id="btnRemove" value="Remove"></td>';  

Should change to: 应更改为:

echo '<span id="qtyRemoveLink"><input class="linkbtn btnRemove" type="submit" value="Remove"></td>'; 

And: 和:

$('#btnRemove').click(function() {

Should change to: 应更改为:

$('.btnRemove').click(function() {

Here is a demo: http://jsfiddle.net/rSyCw/ 这是一个演示: http : //jsfiddle.net/rSyCw/

Here is an excellent answer on the site regarding the creation of valid HTML IDs: What are valid values for the id attribute in HTML? 这是网站上有关创建有效HTML ID的一个很好的答案:HTML中id属性的有效值是什么?

Use a class for the button, not an ID. 为按钮使用类,而不是ID。 IDs are unique identifiers (can only be one with that name on the whole page), however classes may be applied to multiple elements. ID是唯一的标识符(在整个页面上只能是一个具有该名称的标识符),但是类可以应用于多个元素。 So... 所以...

<input type="submit" id="btnRemove" />

Becomes

<input type="submit" class="btnRemove" />

And, as such, your selector changes to .btnRemove . 这样,您的选择器将更改为.btnRemove

And, now that you know that IDs need to be unique, you should go ahead and re-configure the output to use classes, or use IDs with a unique ID appended. 而且,既然您知道ID必须是唯一的,则应该继续并重新配置输出以使用类,或使用附加了唯一ID的ID。 eg 例如

<?php
  while ($row = $result->fetch()){
?>
    <!-- Other HTML -->
    <input type="submit" id="btnSubmit_<?= $row['Unique_Column_Id']; ?>" ... />

Then you can modify your selector: 然后,您可以修改选择器:

$('[id^="btnSubmit_"]').click(...); // All elements with an
                                    // ID that starts with "btnSubmit_"

One red flag here is that you are generating multiple elements with the same ID on each iteration of your loop. 这里的一个危险信号是您在循环的每次迭代中都生成具有相同ID的多个元素。 Element IDs need to be unique in your document. 元素ID在您的文档中必须唯一。 My guess is that jQuery is only binding to the first element it finds with the ID "btnRemove" because it assumes that your IDs are unique. 我的猜测是,jQuery仅绑定到ID为“ btnRemove”的找到的第一个元素,因为它假定您的ID是唯一的。

As others have stated, using a class name on your button would help your problem, but you should still come up with a way to put unique IDs on your itemName , qtyNum , qtyRemoveLink , orderStatus , itemPriceRow , and itemPrice elements, and anywhere else you might be using this technique. 正如其他人所述,在按钮上使用类名将有助于解决问题,但是您仍然应该想出一种将唯一ID放在itemNameqtyNumqtyRemoveLinkorderStatusitemPriceRowitemPrice元素以及其他任何位置的方法。可能正在使用此技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM