javascript
  • jquery
  • 2013-06-29 19 views -1 likes 
    -1

    我有這個函數創建一個元素。點擊功能不能正常工作時,該事件由另一個函數創建的元素稱爲

    function AddPayment(ID) 
    { 
        showForm = $('#AddPaymentForm').html();  
        $('#divAJAX_'+ID).html(showForm); 
    
        $(".cancel").click(function(){ AddPayment(ID); }); 
    } 
    

    從這個

    <div id='AddPaymentForm'>  
         <span class='button' id='cancel' class='cancel' >Cancel</span> 
    </div> 
    

    來的時候想的是功能的地方在這裏的元素

    <div id='divAJAX_ID'></div> 
    

    我也希望這個函數創建一個onclick函數o我的跨度,但它不工作。 我想這個問題是由放置

    $(".cancel").click(function(){ AddPayment(ID); }); 
    

    在錯誤的位置來了。我嘗試了所有可能的地方,但我仍然無法正確工作。

    怎麼了?

    +0

    你能做一個小提琴(http://jsfiddle.net/)嗎?這會有很大的幫助。 – slacktracer

    +0

    我對事件處理程序中的'AddPayment(ID)'感到困惑。點擊「取消」將覆蓋現有的表單,即重置每個字段的值(我假設你在'#AddPaymentForm'中有更多的元素)。這是你打算做什麼? –

    +0

    '

    ',這裏的ID是動態的或靜態的,因爲我看到'$('#divAJAX _'+ ID)之類的東西。html(showForm);'(是'#divAJAX_ID') –

    回答

    2

    你有兩類屬性在相同的元素。它應該是這樣的:的

    class="button cancel" 
    

    代替

    class="button" id="whatever" class="cancel" 
    

    這可能是造成麻煩的jQuery。

    看看它是如何開始進行這項工作的小提琴:http://jsfiddle.net/pvNrg/2/

    首先,你的HTML問題:

    <div id='AddPaymentForm'> 
        <p>Coming from this</p> 
    </div> 
    
    <span id='cancel' class='cancel'>Cancel</span> 
    
    <p>I wanted that function to place the element in here</p> 
    
    <div id='divAJAX_ID'></div> 
    
    <p>I also wanted that function to create an onclick function on my span, but it isn't working. I guess the problem is coming from placing the ... at the wrong placement. I've tried all the possible place but I can't still work this right.</p> 
    
    <p>What's wrong?</p> 
    

    和JavaScript:

    $(function() { 
    
        function AddPayment(ID) { 
         showForm = $('#AddPaymentForm').html(); 
         $('#divAJAX_' + ID).html(showForm); 
        } 
    
        $(".cancel").click(function() { 
         AddPayment('ID'); 
        }); 
    

    });

    +0

    哦greate @ jaywalking101我沒有注意到,我在不同的地方包括兩個班。我只是將它們添加到相同的持有人,然後它工作正常。非常感謝! –

    +0

    好,但您的代碼示例與OP沒有達到相同的效果。他們使用事件處理程序中'AddPayment'函數的'ID',而使用字符串''ID''。 –

    +0

    @ ivory- santos:這段代碼還創建了一個'.cancel'並插入到$('#divAJAX_'+ ID)'中。我認爲'$('#divAJAX_'+ ID)'內的'.cancel'不起作用。這是個問題嗎? –

    1

    對於動態創建的元素,你必須做event delegation

    $(document).on("click", ".cancel" , function(event){ 
        alert($(this).text()); 
    }); 
    
    0
    $("#AddPaymentForm,[id^=divAJAX_]").on("click", ".cancel" , function(event){ 
        alert($(this).text()); 
    }); 
    

    連接只委派的事件處理程序的容器,你需要監視列表(不是文檔),如果您需要保存性能(你不需要監視所有的.cancel)。採用這種方法,該活動不必冒泡更多層次。如果您有另一個元素,它是AddPaymentForm的父項,並且全部爲divAJAX_。你可以這樣寫:

    $("#anotherContainer").on("click", ".cancel" , function(event){ 
         alert($(this).text()); 
        }); 
    
    +0

    OP沒有將數據設置爲$('#AddPaymentForm')並將其設置爲$('#divAJAX _'+ ID),這是一個動態ID。 –

    0

    使用此代碼

    function AddPayment(ID) 
    { 
        var showForm = $('#AddPaymentForm').html(); 
        var container = $('#divAJAX_'+ID); 
    
        container.html(showForm); 
        container.find(".cancel").click(function(){ AddPayment(ID); }); 
    } 
    
    相關問題