jQuery 使用 HTML DOM 将 onclick 事件分配给单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18974632/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:53:16  来源:igfitidea点击:

Assign onclick Events to radiobutton Using the HTML DOM

jqueryonclickradio-buttononload

提问by Blossom

<input type="radio" name="sam1" value="1">HI
<input type="radio" name="sam1" value="2">HI HI

Actually i am try to add on click event to the above mention radio button

实际上我尝试将点击事件添加到上面提到的单选按钮

$("input:radio[name=sam1]").onclick = function () {disp()};

Please correct my code

请更正我的代码

function disp(){//content here}

回答by Arun P Johny

it is

这是

//dom ready handler
jQuery(function ($) {
    $("input:radio[name=sam1]").click(disp)
})

The onlickproperty is used when you are trying to add a event handler to a dom element reference, here $("input:radio[name=sam1]")is a jQuery wrapper so you can use the event utilities provided by jQuery to register the event handler like .on()

onlick当您尝试将事件处理程序添加到 dom 元素引用时,将使用该属性,这$("input:radio[name=sam1]")是一个 jQuery 包装器,因此您可以使用 jQuery 提供的事件实用程序来注册事件处理程序,如.on()

Demo: Fiddle

演示:小提琴

回答by Outlooker

Hope this link might help you.. :)

希望这个链接可以帮助你.. :)

$("input:radio[name=sam1]").click(function(){
 disp();
});

http://jsfiddle.net/nibin_10/JNyAB/

http://jsfiddle.net/nibin_10/JNyAB/

回答by Nishu Tayal

You are mixing jquery and javascript here.

您在这里混合使用 jquery 和 javascript。

Use like this :

像这样使用:

function disp(){
    //content here
}
$("input:radio[name=sam1]").click(disp)

Here is the demo :http://jsfiddle.net/Nz4ET/

这是演示:http: //jsfiddle.net/Nz4ET/

回答by Sukhchain Singh

Try this
<input type="radio" name="sam1" value="1">HI

<input type="radio" name="sam1" value="2">HI HI

<script>
$('input:radio[name=sam1]').click(function(){
    disp()
});
function disp()
{
    alert('Hello'); 
}
  </script>