简体   繁体   English

默认情况下选中单选按钮-JQuery事件不起作用

[英]Radio button checked by default - JQuery event not working

I have a radio button already clicked in html, and I need some help figuring out how to apply the event in JQuery for the already clicked button. 我已经在html中单击了一个单选按钮,并且我需要一些帮助来弄清楚如何在JQuery中为已单击的按钮应用事件。 Right now, if the button is already clicked there is no event happening, but if i click again it does what it's supposed to do. 现在,如果已经单击了按钮,则不会发生任何事件,但是如果我再次单击,它将执行它应该执行的操作。 I have 2 radio buttons, the first one is already clicked for a message in a div to be hidden, is the second radio is checked then the message with show. 我有2个单选按钮,第一个单选按钮已经被单击以隐藏div中的消息,第二个单选按钮被选中,然后显示消息。 I have problems implementing the already clicked radio. 我在执行已单击的收音机时遇到问题。 This is the html: 这是html:

<label for="answer">Answers:</label><input type="radio" name="type" value="Yes/No" class="answers" checked="checked" id="answer"><span> Yes/No Form </span> <br><input type="radio" name="type1" value="Multiple" class="move"> <span>Multiple choice form </span><br>

And this is the JQuery so far: 到目前为止,这是JQuery:

$(".answers").click(function(e) {
    e.preventDefault();
    $("div.desc").hide();
}); 

$(".move").click(function(e) {
    e.preventDefault();
    $("div.desc").show();
}); 

If anyone can please help, thank you! 如果有人可以帮助,谢谢!

Since you are wanting to hide the div on page load, just utilize CSS. 由于您要在页面加载时隐藏div ,因此只需利用CSS。

Add a display to your CSS class: 向您的CSS类添加display

div.desc {
    display: none;
}

Or to the div element itself. 或到div元素本身。

<div class="desc" style="display: none;">omg</div>

This will hide the element on page load, allowing you to show/hide the form as needed. 这将在页面加载时隐藏元素,使您可以根据需要显示/隐藏表单。

For completeness, here is a JSFiddle . 为了完整起见 ,这里有一个JSFiddle

there are multiple option to hide $("div.desc") at the time of page : 在页面显示时,有多个选项可以隐藏$(“ div.desc”):

1). 1)。 at the time of DOM ready just call a trigger event, it will fire a click event like: 在DOM准备就绪时,只需调用一个触发事件,它将触发一个click事件,例如:

$(".answers").trigger("click")

OR 要么

2). 2)。 just add a simple condition to explicitly hide the content on page load like: 只需添加一个简单条件即可显式隐藏页面加载时的内容,例如:

$("div.desc").hide();

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

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