English 中文(简体)
如何在 Jquery 的表格行中添加按钮
原标题:How do I add a button to a table row in Jquery
  • 时间:2012-05-25 11:02:36
  •  标签:
  • jquery

我准备了一个快速和肮脏的,直到申请酒吧。

当用户单击饮料类型的按钮时,会将一行加到表格中,并加上一个+和 - 以使用户能够更改数量。

$(".drinkButton").click(function() {

    var destTable = $("#trTable");
    var drinkID = $(this).attr("id");

    switch (drinkID) {
    case  becks :
        var dName = "Becks";
        var dPrice = "3.00";
        break;
    case  corona :
        var dName = "Corona";
        var dPrice = "3.00";
        break;
    }

    var newRow = $("<tr><td>" + dName + 
                           "</td><td>" + dPrice + "</td><td>1</td> 
                           <td><input id= Button  type= button  value= -  class= minusButton  /></td> 
                           <td><input id= Button  type= button  value= +  class= plusButton  /></td> 
                           <td>" + dPrice + "</td> 
                  </tr>");

$("#trTable").append(newRow);
    $(this).attr( disabled ,  disabled );
});

$(".plusButton").click(function() {
    alert( Plus Button Clicked );
});​

此功能有效, 但添加的增减按钮不会点燃事件 。

我做错什么了?

问题回答

您的代码不起作用, 因为使用 kick under > 或 unbind 添加的事件只执行一次, 并且只对当前 DOM 执行一次, 而对于以后添加的元素, 您可以在添加新的 . plus button 时重置事件, 或者使用 on , 这将自动为您处理此事 。

"http://api.jquery.com/on/" rel="no follow">docs

$(document).on( click ,  .plusButton , function() {
   // alert etc
});
$(".plusButton").live( click , function() {
    alert( Plus Button Clicked );
});​

//Or

$(".plusButton").on( click , function() {
    alert( Plus Button Clicked );
});​

问题是您正在加载页面后添加一个 DOM 对象( 表), 并在该 DOM 对象上添加单击事件 。 它不会工作, 因为$( document) 发射该 dom 对象时$. ready () 没有出现。 因此您可以使用下面给出的两个解决方案中的任何一个 。

$(".plusButton").on( click , function() {
    alert();
}); 

$(".plusButton").live( click , function() {
    alert();
});调

在您创建了您的新Row元素之后, 您可以像这样绑定点击事件 :

$( .plusButton , newRow).click(function() {
    alert( Plus Button Clicked );
});

Edit: You can also use a on delegated event handler as shown in NiftyDude s post. live is deprecated, on should be used instead (thanks VisioN for pointing out this)





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...