IMPORTANT!: I have to use onclick attribute. Using Jquery on event listener/handler is not an option in my case. Why? I have a lot of other things in my code that are dependent on the attribute and its manifestation in the code.
When putting alert in onclick
attribute it's working as expected and the alert is shown in a popup window and return false prevents the form from being submitted by pressing the button:
<script>
$().ready(function() {
$('.pretty .prettycheckbox').click(function () {
var myType = $('.pretty .prettycheckbox').find("input[class^=car-type]:checked").val();
if (myType == 1) {
$('#sendButton').attr("onclick", "alert('Direct!'); return false;");
}
if (myType == 2) {
$('#sendButton').removeAttr("onclick");
}
});
});
</script>
But it's not working when calling a function from onclick atttribute like:
<script>
$().ready(function() {
$('.pretty .prettycheckbox').click(function () {
var myType = $('.pretty .prettycheckbox').find("input[class^=car-type]:checked").val();
if (myType == 1) {
$('#sendButton').attr("onclick", "submitDB(); return false;");
}
if (myType == 2) {
$('#sendButton').removeAttr("onclick");
}
});
});
</script>
<script>
$().ready(function(){
function submitDB() {
alert('From function!');
}
});
</script>
Any idea why?
Why is the function ignored?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…