繁体   English   中英

如何获取在Jquery文档就绪功能中触发的复选框单击事件?

[英]How to get the check box click event fired in Jquery document ready function?

document.ready函数中,警报引发undefined错误消息。 在此复选框上,我将必须启用或禁用单选按钮控件。

如何验证,是否复选框:other被选中?

$(document).ready(function () 
{
  $("#Other").on("click", function ()
    {
       // On this check box click event, i will have to enable radio buttons 
       document.getElementById('Yes').disabled = false;
       document.getElementById('No').disabled = false;
    });
});

要在document.ready上触发click事件,您需要触发在element上定义的click事件。

$(document).ready(function() {
    $("#Other").on("click", function() {
        alert($(this).val());
    });
    $("#Other").trigger("click");
});

在上述#Other上的click事件中已定义。

 $(document).ready(function() { $("#Other").on("click", function() { alert($(this).val()); }); $("#Other").trigger("click"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="Other" type="checkbox" value="Hello World">checkbox 

要检查复选框是否被选中,您可以执行

if ($("#Other").is(":checked")) {
    // do something if the checkbox is checked
}

 $(document).ready(function() { if ($("#Other").is(":checked")) { $('input[type="radio"]').prop('checked', true); } else { $('input[type="radio"]').prop('checked', false); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="Other" type="checkbox" value="Hello World" checked>checkbox1 <br> <input type="radio" name="gender" value="radio"> radio<br> 

要选中和取消选中该复选框,您的代码如下所示:

 $(document).ready(function() { $("#Other").on("click", function() { if ($(this).is(":checked")) { $('input[type="radio"]').prop('checked', true); } else { $('input[type="radio"]').prop('checked', false); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="Other" type="checkbox" value="Hello World">checkbox1 <br> <input type="radio" name="gender" value="radio"> radio<br> 

要启用和禁用单选按钮,可以添加禁用属性。

 $(document).ready(function() { $("#Other").on("click", function() { enblDsblChkb($(this)); }); enblDsblChkb($("#Other")); function enblDsblChkb($elem){ if ($elem.is(":checked")) { $('input[type="radio"]').prop('disabled', false); } else { $('input[type="radio"]').prop('disabled', true); } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="Other" type="checkbox" value="Hello World">checkbox1 <br> <input type="radio" name="gender" value="yes"> Yes<br> <input type="radio" name="gender" value="no"> No<br> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM