JQuery change function not working in safari brows

2019-08-27 03:42发布

问题:

I have a radio button and checkbox

<input type='radio' name='testradiochange' id='testradiochange'> Testing Radio Change</input>

<input type='checkbox' name='acknowledge' id='acknowledge' value='acknowledge'></input>

I want to be notified when these buttons are selected

$("input[name=testradiochange]:radio").change(function () {
    alert('radio selected');    
}

$("input[name=acknowledge]:checkbox").change(function () {
    alert('checkbox selected');
 }

The above works fine in Chrome and Firefox but not in Safari. Any idea?

回答1:

You have lots of typos in your code, here are they :

$("input[name=testradiochange]:radio").change(function () {
    alert('radio selected');    
}); // <= it was just }, i added ); to close the function's (

$("input[name=acknowledge]:checkbox").change(function () {
    if ($(this).is(':checked')) { // if check
    	alert('checkbox selected');    
    } else {                      // if uncheck
    	alert('checkbox unselected');
    }
 }); // <= it was just }, i added ); to close the function's (
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label><input type='radio' name='testradiochange' id='testradiochange'/>Testing Radio Change</label>
<!-- inputs don't have closing tags, they are self-closing, if you want to display text next to them, use a label -->
<input type='checkbox' name='acknowledge' id='acknowledge' value='acknowledge'/>