可以't使用jquery获取单选按钮的值

Can't get value of radio buttons using jquery

本文关键字:获取 单选按钮 jquery 使用 可以      更新时间:2023-09-26

我有一系列单选按钮和一个提交按钮:

<input type="radio" name="selectme_resubmit" id="selectme_resubmit1" value="first" /> <label for="selectme_resubmit1">Resubmit with the original submitter</label><br>
<input type="radio" name="selectme_resubmit" id="selectme_resubmit2" value="without" checked /> <label for="selectme_resubmit2">Resubmit without any submitter</label><br>
<input type="radio" name="selectme_resubmit" id="selectme_resubmit3" value="custom" /> <label for="selectme_resubmit3">Resubmit with a custom submitter:</label> <input type="text" name="selectme_custom_submitter" id="selectme_custom_submitter" /><br>
<input type="button" id="selectme_resubmit_button" name="selectme_resubmit2_button" value="Place a submit template" onclick="selectme_act(''resubmit'')" />

我还有以下javascript(带有jquery):

var typeofsubmit = $("input[name=selectme_resubmit]:checked").val();
console.log(typeofsubmit);

然而,我在控制台中得到了"未定义",即使我选择了一些东西。。。我做错了什么?

我测试了您的代码,选择器确实工作了。我发现的唯一问题是,当onclick事件中不需要'字符时,您正在转义该字符

您错过了就绪功能

$().ready(function(){
 var typeofsubmit = $("input[name=selectme_resubmit]:checked").val();
  console.log(typeofsubmit);
 });

请在申报之前使用就绪功能

并检查此链接http://jsfiddle.net/FQGuu/