javascript 在页面加载期间触发选中单选按钮的点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19138618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
triggering a click event for checked radio button during page load
提问by acr
I have two set of radio button in my html form and I am displaying a hidden div depends upon the radio button selection.And the hidden div ONLYdisplay if both radio buttons are clicked.I am using a jquery to trigger click if the radio buttons are clicked
我有两个组单选按钮,在我的HTML表单,我显示隐藏的div取决于单选按钮selection.And隐藏的div只显示如果两个单选按钮clicked.I正在使用jQuery来触发点击,如果单选按钮被点击
But when ever I load the page, the buttons are getting checked as expected and it not triggering the click event. due to which a user has to check the radio buttons again(eventhough they are selected).
但是当我加载页面时,按钮会按预期进行检查,并且不会触发点击事件。因此用户必须再次检查单选按钮(即使它们被选中)。
My html code for radio buttons:
我的单选按钮的 html 代码:
<div class="block">
<input onClick="show_seq_lunid();" type="radio" name="button1" value="Yes" <?php if(!isset($_POST['button1']) || (isset($_POST['button1']) && $_POST['button1'] == 'Yes')) echo ' checked="checked"'?> checked /><label>Yes</label>
<input onClick="show_list_lunid();" type="radio" name="button1" value="No" <?php if(isset($_POST['button1']) && $_POST['button1'] == 'No') echo ' checked="checked"';?> /><label>No</label>
</div>
<div class="block">
<input onClick="os_others();" type="radio" name="button2" value="Yes" <?php if(!isset($_POST['button2']) || (isset($_POST['button2']) && $_POST['button2'] == 'Yes')) echo ' checked="checked"'?> checked /><label>Others</label>
<input onClick="os_hpux();" type="radio" name="button2" value="No" <?php if(isset($_POST['button2']) && $_POST['button2'] == 'No') echo ' checked="checked"';?> /><label>HP-UNIX</label>
</div>
And I tried below code to trigger click event for a checked radio button
我尝试了下面的代码来触发选中的单选按钮的点击事件
$('input:radio[name=button1]:checked').click();
$('input:radio[name=button2]:checked').click();
but it is not working. How can I trigger the click event ?
但它不起作用。如何触发点击事件?
Update: Tried in DOM ready mode using code below, but no luck
更新:使用下面的代码在 DOM 就绪模式下尝试,但没有运气
$(document).ready(function() {
$('input:radio[name=button1]:checked').click();
$('input:radio[name=button2]:checked').click();
});
回答by carter
if($('input[name="button1"]').is(':checked')){
alert('1 is checked');
}
if($('input[name="button2"]').is(':checked')){
alert('2 is checked');
}
The above works, but I noticed that jsfiddle doesnt like function calls in the onClick attribute. The below worked in my browser but not on jsfiddle. It doesnt see the function being defined somehow.
以上工作,但我注意到 jsfiddle 不喜欢 onClick 属性中的函数调用。以下内容在我的浏览器中有效,但在 jsfiddle 中无效。它没有看到以某种方式定义的函数。
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<div class="block">
<input onClick="myOtherAlert();" type="radio" name="button1" value="Yes" checked /><label>Yes</label>
<input onClick="myOtherAlert();" type="radio" name="button1" value="No" /><label>No</label>
</div>
<div class="block">
<input onClick="myAlert();" type="radio" name="button2" value="Yes" checked /><label>Others</label>
<input onClick="myAlert();" type="radio" name="button2" value="No" /><label>HP-UNIX</label>
</div>
<script>
if($('input[name="button1"]').is(':checked')){
$('input[name="button1"]:checked').trigger('click');
}
if($('input[name="button2"]').is(':checked')){
$('input[name="button2"]:checked').trigger('click');
}
function myAlert(){
alert('it worked');
}
function myOtherAlert(){
alert('see');
}
</script>
</body>
</html>