简体   繁体   English

动态添加的按钮,jQuery无法正常工作

[英]Dynamically Added button, jquery not working

I've been going through my dynamically added code and changing everything that was using .click() to use .on("click") and its been working great but now I've run into something that I am not sure how to convert to dynamically added compatible. 我已经遍历了动态添加的代码,并将使用.click()的所有内容更改为使用.on(“ click”)的所有内容,并且效果很好,但是现在我遇到了不确定如何转换的内容动态添加兼容。

Here is the code: This is a function in javascript that is called when a button named "Add" or "Remove" is clicked (these buttons are also dynamically added after another button is clicked) 这是代码:这是javascript中的一个函数,当单击名为“添加”或“删除”的按钮时(这些按钮也会在另一个按钮被单击后动态添加)时被调用

    function row_add_remove(sname, snum, count, type) {
    if (type == "add") {
        var selectbox = '<select id="qty_'+snum+'_'+count+'" name="qty_'+snum+'_'+count+'">';
        for (i=1;i<16;i++){selectbox += '<option value='+i+'>'+i+'</option>';}
        selectbox += '</select>';
        if (count > 1) {
            $("#tr_"+sname+"_"+(count-1)).after('<tr id="tr_'+sname+'_'+count+'"><td>'+selectbox+'&nbsp;&nbsp;&nbsp;X&nbsp;&nbsp;&nbsp;<input type=text id="item_'+sname+'_'+count+'" name="item_'+snum+'_'+count+'" size="70" placeholder="Item '+count+'" /></td></tr>');
            $("#toprow"+snum+"_count").html("("+count+")");
        }
    }
    if (type == "remove") {
        if (count == 1) {
            $("#"+sname).hide();
            $("#toprow"+snum+"_count").html("");
            $("#td_"+sname+"_"+count).remove();
            $("#ph_order_div").show();
            count--;
        }
        if (count > 1) {
            $("#tr_"+sname+"_"+count).remove();
            count--;
            $("#toprow"+snum+"_count").html("("+count+")");
        }
        if (count < 0) {count = 0;}
    }
}

The problem (I think, maybe its more than this) is the .after() doesn't fire. 问题(我想,也许还不止如此)是.after()无法触发。 Now i'm not sure if the problem lies with that or with the whole code but when I click the Add button its not adding another TR after the one that is shown by default. 现在,我不确定问题是否出在此或整个代码中,但是当我单击“添加”按钮时,它不会在默认显示的TR之后添加另一个TR。

This code works great when NOT dynamically added so can anyone help me with what I need to change to get the above function to work when the buttons that call it and the TR's that it tries to modify are dynamically added? 如果未动态添加此代码,则效果很好,因此,当动态添加调用它的按钮及其尝试修改的TR时,有人可以帮助我完成上述功能,以便我实现上述功能吗?

Here is the code that is fired when the buttons are clicked: 这是单击按钮时触发的代码:

    whatmeatfield = $("#meat_field_count").html();      
$(document).on("click", "#add_btn_1", function() {
    if (whatmeatfield <= 0) {whatmeatfield = 1;}
    whatmeatfield++;
    row_add_remove("meatseafood",1,whatmeatfield,"add");
});
$(document).on("click", "#remove_btn_1", function() {
    row_add_remove("meatseafood",1,whatmeatfield,"remove");
    whatmeatfield--;
    if (whatmeatfield <=0) {whatmeatfield = 1;}
});

in your .after() , there is typo mistake in <input type=text there should be quote like <input type="text" 在您的.after()<input type=text有拼写错误,应该包含诸如<input type="text"引号

You have not define whatmeatfield variable. 您尚未定义whatmeatfield变量。 it should be 它应该是

var whatmeatfield

Also you are directly passing html of some element id, you need to parse it to integer . 另外,您直接传递某些元素ID的html,则需要将其parseinteger like 喜欢

var whatmeatfield = parseInt($("#meat_field_count").html()) ;

This may help you to solve your issues 这可以帮助您解决问题

The problem may occur becouse of two things. 该问题可能是由于两件事引起的。

  1. You're using id selector and you're porobably adding another button with the same id and as far as html spec says id should be unique. 您正在使用ID选择器,并且有可能添加另一个具有相同ID的按钮,并且据html spec指出ID应该是唯一的。

What can you do? 你能做什么? Change ID to CLASS. 将ID更改为CLASS。

  1. The problem occurs becouse you're changing the id to schema like #remove_btn_1, #remove_btn_2, #remove_btn_3 and therefore you do not have any handler for this buttons. 发生问题是因为您将ID更改为#remove_btn_1,#remove_btn_2,#remove_btn_3之类的架构,因此您没有用于此按钮的任何处理程序。

What can you do? 你能做什么? Change selector from "#remove_btn_1" to "[id*=remove_btn_]" 将选择器从“#remove_btn_1”更改为“ [id * = remove_btn_]”

Of course same with #add_btn_ 当然与#add_btn_相同

Post me back if it was helpful. 如果有帮助,请发回我。

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

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