如何在 Jquery 文档就绪函数中触发复选框单击事件?
How to get the check box click event fired in Jquery document ready function?
在 document.ready
函数中,警报抛出 undefined
错误消息。
在此复选框选择中,我将不得不启用或禁用单选按钮控件。
如何验证checkbox : 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
上触发点击事件,您需要触发您在元素上定义的点击事件。
$(document).ready(function() {
$("#Other").on("click", function() {
alert($(this).val());
});
$("#Other").trigger("click");
});
在上面#Other
上的点击事件已经定义。
$(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>
在 document.ready
函数中,警报抛出 undefined
错误消息。
在此复选框选择中,我将不得不启用或禁用单选按钮控件。
如何验证checkbox : 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
上触发点击事件,您需要触发您在元素上定义的点击事件。
$(document).ready(function() {
$("#Other").on("click", function() {
alert($(this).val());
});
$("#Other").trigger("click");
});
在上面#Other
上的点击事件已经定义。
$(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>