问题

我试图使这样做,如果我们的页面上的复选框被选中,它将显示一个警告消息,通知他们选择了显示他们的结帐历史的用户.如果用户取消选中该复选框,则应显示另一个提醒框,让他们知道他们选择不显示其结帐历史记录.如果勾选/取消选中此复选框,我无法正常显示提示框.这是我的代码.

HTML

<div class="myAccountCheckboxHolder" id="showCheckoutHistoryCheckbox">
    <input tabindex="40" checked="checked" id="showCheckoutHistory" name="showCheckoutHistory" type="checkbox">
        <label for="showCheckoutHistory" class="checkLabel">Show my checkout history</label>
    </div>    

的Javascript

function validate() {
    var checkoutHistory = document.getElementById('showCheckoutHistory');
    if (checkoutHistory.checked) {
        alert("You have elected to show your checkout history.");
    } else {
        alert("You have elected to turn off checkout history.");
    }

谢谢.



解决方法
$(document).ready(function() {
    $('#showCheckoutHistory').change(function() {
        if ($(this).prop('checked')) {
            alert("You have elected to show your checkout history."); //checked
        }
        else {
            alert("You have elected to turn off checkout history."); //not checked
        }
    });
});

希望它有帮助;).

PS.文档资料: http://api.jquery.com/prop/




相关问题推荐