2016-04-11 20 views
3

İşte benim sorunum:Değiştirilen öğede olayın nasıl yeniden adlandırılacağı

Onclick olayı ayarladığım bir balerin var. Ve benim özelliğimi bitirdiğimde, hatırlayamıyorum.

$('.editAction').click(function (event) { 
//Some stuff 
handleEditThemeAjax(this, themeId, nomThemeEn, nomThemeFr); 
//Call to another function 
return false; //Do not redirect to URL 
}); 

function handleEditThemeAjax(bouton, themeId, nomEn, nomFr) { 
    $(bouton).unbind("click"); 
    $(bouton).removeClass("editAction"); //To not get another call to the parent function 

//unset url to avoid being redirected 
bouton.removeAttribute("href"); 

//Some stuff and declaration 
$(bouton).click(function() { 
    //AJAX Handle 
    $.ajax({ 
     type: "POST", 
     url: "{{ path('editThemeAjax') }}", //Twig 
     data: { 
      "nomFr": newNomFr, 
      "nomEn": newNomEn, 
      "themeId": themeId 
     }, 
     success: function (data) { 
      setInputToText(tdFr, newNomFr); 
      setInputToText(tdEn, newNomEn); 
      Materialize.toast(data, 2000, 'rounded'); 
     }, 
     error: function (data) { 
      console.log(data); 
      Materialize.toast("Une erreur est survenue lors de l'édition de la thématique"); 
     } 
    }); 
} 

//Let's put the button to it initial state 

var tr = tdEn.parentElement; 
lineButton = tr.childNodes[7]; 

//The button 
var boutonDone = lineButton.childNodes[1]; 
$(boutonDone).removeClass("green"); 
$(boutonDone).addClass('blue editAction'); 
boutonDone.childNodes[1].innerHTML = "mode_edit"; 

// Clone the button to remove all previous event 
var clone = boutonDone.cloneNode(); 
while (boutonDone.firstChild) { 
    clone.appendChild(boutonDone.lastChild); 
} 

//replace it 
boutonDone.parentNode.replaceChild(clone, boutonDone); 

clone.href = url; 
//Set the url again 

//Now, if i click on my button again, nothing append. 
//I would like that if i clicked again, my first function is executed (the "$('.editAction').click(...)") 

}); 
return false; //avoid being redirected 
} 

Ne ben bunu düzeltmek için ne yapabilirim: Burada

benim kod parçası mı?

Yardımlarınız için şimdiden teşekkür ederiz!

+0

Sana aslında ne istediklerini net değil. Yalnızca, tıklama etkinliğinde, tıklama etkinliğini yeniden ayarladığınızı anladım. Sonra? – Bharadwaj

+0

Sonra başka bir olay ayarlıyorum, bu yüzden ilkini devre dışı bırakmalıyım. İkinci etkinliğin tetiklenmesinden sonra, ilk etkinliği etkinleştirmek istiyorum ancak yapamam. – Sylvain

+0

daha sonra ikinci olay tetikleyicisine bağla! – Bharadwaj

cevap

1

Tıklama olay ayarlayıcınızı bir işleve sarmayı ve gerçekten ihtiyacınız olduğunda tekrar çağırmanızı öneririm.

var set_editAction_click_events = function (themeId, nomThemeEn, nomThemeFr) { 
    $('.editAction').click(function (event) { 
     //Some stuff 
     handleEditThemeAjax(this, themeId, nomThemeEn, nomThemeFr); 
     //Call to another function 
     return false; //Do not redirect to URL 
    }); 
} 

// ... your code ... 

//The button 
var boutonDone = lineButton.childNodes[1]; 
$(boutonDone).removeClass("green"); 
$(boutonDone).addClass('blue editAction'); 
boutonDone.childNodes[1].innerHTML = "mode_edit"; 

// Set click events again 
set_editAction_click_events(themeId, nomThemeEn, nomThemeFr); 

Veya bir işlev kodu sarmak için istemiyorsanız:

$(boutonDone).click(function() { 
    handleEditThemeAjax(this, themeId, nomThemeEn, nomThemeFr); 
    //Call to another function 
    return false; //Do not redirect to URL 
}); 
+0

Teşekkürler! Mükemmel çalışıyor – Sylvain

İlgili konular