簡體   English   中英

將事件綁定到jQuery Modal Pop Up中的動態添加的元素

[英]Binding events to dynamically added elements in jQuery Modal Pop Up

我正在使用jQuery Modal PoUp 我在彈出窗口中顯示了許多文本框和按鈕。 我想向其中添加一個單擊事件btnaddlot按鈕,但是無法這樣做。

var varLot = '<b>Lot Title: </b> <input type="text" id="txtlottitle" style="width:500px;" value="Lot Title" /> <textarea id="lotstextarea"></textarea> <input type="submit" value="ADD" id="btnAddLot" class="bluebutton" style="float:right;"/> <br /> <div id="lottypes"> <table> <tr> <td style="width:width: 80px;"><b>Lot Types: </b></td><td> <table> <tr><td><table><tr><td><input id="rdoitemlot" name="lottype" type="radio" value="Item Lot" /></td><td>Item Lot - Bid at Item level, compete at Lot level(collect item pricing during bidding.)</td></tr></table> </td></tr> <tr><td><table><tr><td><input id="rdobasketlot" name="lottype" type="radio" value="Basket"/></td><td>Basket - Bid at Lot level, compete Lot level(collect item pricing post bidding.)</td></tr></table> </td></tr> <tr><td><table><tr><td><input id="rdobasketwithnoitem" name="lottype" type="radio" value="Basket with No Items" /></td><td> Basket with No Items - Bid at Lot level, compete Lot level(Do not collect item pricing.)</td></tr></table> </td></tr> </table></td> </tr> </table> </div> <hr /> <table> <tr> <td>Improve Bid By: </td><td><span id="lblimprovebidsby"></span></td> </tr> <tr> <td><span id="lblbiddecrement">Bid Decrement</span></td><td><input type="number" id="txtbiddecrement" /></td> </tr> <tr> <td><span >Protect the lead bid with front buffer of : </span></td><td><input type="number" id="txtfrontbuffer" /></td> </tr> <tr> <td><span >Protect the lead bid with back buffer of : </span></td><td><input type="number" id="txtbackbuffer" /></td> </tr> <tr> <td><span >Can participants submit tie bids : </span></td><td><span id="lbltiebids"></span></td> </tr> </table>'

var section = $(this);
$(this).toggleClass("expand");
$(function () {
    $("#dialog").dialog(
        { width: 800 }, 
        { height: 600 }, 
        { modal: true }, 
        { 
            open: function (event, ui) {
                $("#dialog").html(varLot);
                $("textarea").jqte();
                //$("#dialog").append($(section).html());
            },
            buttons: { 
                "OK": function () { 
                    $(this).dialog("close"); 
                } 
            } 
        }
    );

似乎添加按鈕的唯一方法就像buttons: { "OK": function () { $(this).dialog("close"); } } buttons: { "OK": function () { $(this).dialog("close"); } }但我想點擊事件添加到btnAddLot以后可以上我可能想的事件添加到我有彈出窗口中的單選按鈕。 我該怎么做呢?

$('#pricingdiv').on('click', '#btnAddLot', function (e) {
    alert('hey');
    e.preventDefault();   

});

只需將html作為常規代碼添加,然后就可以直接設置onclick了。 如果需要,首先將彈出窗口設置為隱藏。

 $('#btnAddLot').click(function (e) { alert('hey'); e.preventDefault(); }); $("#dialog").dialog({ width: 800 }, { height: 600 }, { modal: true }, { open: function (event, ui) { $('#dialog').show(); $("#dialog").html(varLot); $("textarea").jqte(); //$("#dialog").append($(section).html()); }, buttons: { "OK": function () { $(this).dialog("close"); } } }); 
 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <div id='dialog' style="display:none"><b>Lot Title: </b> <input type="text" id="txtlottitle" style="width:500px;" value="Lot Title" /> <textarea id="lotstextarea"></textarea> <input type="submit" value="ADD" id="btnAddLot" class="bluebutton" style="float:right;" /> <br /> <div id="lottypes"> <table> <tr> <td style="width:width: 80px;"><b>Lot Types: </b> </td> <td> <table> <tr> <td> <table> <tr> <td> <input id="rdoitemlot" name="lottype" type="radio" value="Item Lot" /> </td> <td>Item Lot - Bid at Item level, compete at Lot level(collect item pricing during bidding.)</td> </tr> </table> </td> </tr> <tr> <td> <table> <tr> <td> <input id="rdobasketlot" name="lottype" type="radio" value="Basket" /> </td> <td>Basket - Bid at Lot level, compete Lot level(collect item pricing post bidding.)</td> </tr> </table> </td> </tr> <tr> <td> <table> <tr> <td> <input id="rdobasketwithnoitem" name="lottype" type="radio" value="Basket with No Items" /> </td> <td>Basket with No Items - Bid at Lot level, compete Lot level(Do not collect item pricing.)</td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </div> <hr /> <table> <tr> <td>Improve Bid By:</td> <td><span id="lblimprovebidsby"></span> </td> </tr> <tr> <td><span id="lblbiddecrement">Bid Decrement</span> </td> <td> <input type="number" id="txtbiddecrement" /> </td> </tr> <tr> <td><span>Protect the lead bid with front buffer of : </span> </td> <td> <input type="number" id="txtfrontbuffer" /> </td> </tr> <tr> <td><span>Protect the lead bid with back buffer of : </span> </td> <td> <input type="number" id="txtbackbuffer" /> </td> </tr> <tr> <td><span>Can participants submit tie bids : </span> </td> <td><span id="lbltiebids"></span> </td> </tr> </table> </div> 

$(function () {
        var varLot = '<b>Lot Title: </b> <input type="text" id="txtlottitle" style="width:500px;" value="Lot Title" /> <textarea id="lotstextarea"></textarea> <input type="submit" value="ADD" id="btnAddLot" class="bluebutton" style="float:right;"/> <br /> <div id="lottypes"> <table> <tr> <td style="width: 80px;"><b>Lot Types: </b></td><td> <table> <tr><td><table><tr><td><input id="rdoitemlot" name="lottype" type="radio" value="Item Lot" /></td><td>Item Lot - Bid at Item level, compete at Lot level(collect item pricing during bidding.)</td></tr></table> </td></tr> <tr><td><table><tr><td><input id="rdobasketlot" name="lottype" type="radio" value="Basket"/></td><td>Basket - Bid at Lot level, compete Lot level(collect item pricing post bidding.)</td></tr></table> </td></tr> <tr><td><table><tr><td><input id="rdobasketwithnoitem" name="lottype" type="radio" value="Basket with No Items" /></td><td> Basket with No Items - Bid at Lot level, compete Lot level(Do not collect item pricing.)</td></tr></table> </td></tr> </table></td> </tr> </table> </div> <hr /> <table> <tr> <td>Improve Bid By: </td><td><span id="lblimprovebidsby"></span></td> </tr> <tr> <td><span id="lblbiddecrement">Bid Decrement</span></td><td><input type="number" id="txtbiddecrement" /></td> </tr> <tr> <td><span >Protect the lead bid with front buffer of : </span></td><td><input type="number" id="txtfrontbuffer" /></td> </tr> <tr> <td><span >Protect the lead bid with back buffer of : </span></td><td><input type="number" id="txtbackbuffer" /></td> </tr> <tr> <td><span >Can participants submit tie bids : </span></td><td><span id="lbltiebids"></span></td> </tr> </table>'
        $("div#dialog").on("click","#btnAddLot",function () {
            alert('hey');

        });
        $("#dialog").html(varLot).hide().dialog({
        //you might put your options here
        });



    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM