Calling a JavaScript function inside a jQuery generated <a href...>
I have a problem with this piece of code:
$(document).ready(fun开发者_如何学Cction() {
$('.notActiveId').change(function() {
if ($(this).attr('value').length === 0 ) {
$("#notActiveButton").html('');
} else {
$("#notActiveButton").html('<a href="javascript:void(0)" onClick="setStatus(' + $(this).attr('value') + ', activate)" class="operationUnlock" >Activate</a>');
}
});
});
I'm calling with $(this).attr('value')
a value from a select list named notActiveId
. But the problem is how to write $(this).attr('value')
in setStatus()
function, because value of my select is in this form: RZT_83848Rer
(so it consists of characters, underline and numbers).
If I try to write it as above, then I get a JavaScript error.
Instead of using an old-school "onclick", why not add your element via jQuery?
$('#notActiveButton').empty().append($('<a/>', {
href: '#', // the "javascript:" thing is basically bogus and unnecessary
click: function () {
setStatus(this.value, activate);
},
class: 'operationUnlock',
text: 'Activate'
}));
Now, all this assumes that your "notActiveButton" element is some sort of legitimate container for your <a>
tag. But anyway, doing it this way you get to write plain JavaScript without having to worry about the mess of quoting etc.
精彩评论