jquery call to javascript function inside LIVE Never occurs
I am doing an AJAX call that returns some HTML and jQuery:
<div id='selected'>Here is my Selection Text
<a id='trashSelected' href=\"javascript:void(0);\">Remove</a>
</div>
<script>
$('#trashSelected').live('click',function delete()
开发者_开发问答 {
// remove the container
$('#selected').remove();
substractSelectionCount();
return false;
});
</script>
The jQuery removes the container that was added if the user clicks on the link "Remove". It does the job removing the container, but the call to substractSelectionCount(); never occurs. should I be doing the call to this function in a different way?
This is a function that was already in the document. Tested in FF, IE 8 and SafariI might be missing something, but have you tried it this way?
$('#trash{$roleId}').live('click',function delete()
{
// remove the container
substractSelectionCount();
$('#selected').remove();
return false;
});
There seems to be a syntax error in your function callback remove the word delete
$('#trash{$roleId}').live('click',function(e) {
e.preventDefault();
alert('trash clicked');
// ...
return false;
});
A work around for this problem is: instead of returning script from the ajax call, I use classes in my main file. All the response returns is this string
<div id='selected'>Here is my Selection Text
<a class='trashSelected' href=\"javascript:void(0);\">Remove</a>
</div>
and in my file I have a function like this
$('.trashSelected').live('click',function(){
$(this).parent().remove();
subtractSelectionCount();
return false;
});
Is your currentSelectionCount
a global variable? Maybe the function is being called and it's not working because of this?
And did you try adding an alert into the function to make sure it's not being called?
function substractSelectionCount(){
alert("I'm working!");
currentSelectionCount--;
}
I put together a test file in this pastebin and it appeared to be working perfectly with IDs... but since I added three divs to test it I had to replace the IDs with classes. So, I'm not sure where your code is having problems - maybe your substractSelectionCount
function is outside of the document.ready function?
精彩评论