简体   繁体   English

在没有提交按钮的情况下获取选定的单选按钮

[英]Get Selected Radio Button WITHOUT a Submit button

I'm creating radio buttons and I'd like to be able to set the contents of results to the selected button.我正在创建单选按钮,我希望能够将results的内容设置为所选按钮。 WITHOUT a submit button.没有提交按钮。

document.getElementById("results").innerHTML = NONE or A or B

I see a lot of posts on how to do this with a SUBMIT button but I want this to be automatically detected and have struggled to find a relating post.我看到很多关于如何使用“提交”按钮执行此操作的帖子,但我希望能够自动检测到这一点,并且一直在努力寻找相关帖子。 Any help appreciated!任何帮助表示赞赏!

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="btn-group" data-toggle="buttons"> <label id='RADIO' class="btn btn-primary"> <input type="radio" name="test" id="NONE" autocomplete="off">NONE </label> <label class="btn btn-primary"> <input type="radio" name="test" id="A" autocomplete="off">A </label> <label class="btn btn-primary"> <input type="radio" name="test" id="B" autocomplete="off">B </label> </div> <div id="results"></div>

 $( document ).ready(function() { console.log( "ready!" ); $("input[name='test']").change(function(){ $("#results").text($("input[name='test']:checked").val()); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="btn-group" data-toggle="buttons"> <label id='RADIO' class="btn btn-primary"> <input type="radio" name="test" id="testNONE" autocomplete="off" value="NONE">NONE </label> <label class="btn btn-primary"> <input type="radio" name="test" id="testA" autocomplete="off" value="A">A </label> <label class="btn btn-primary"> <input type="radio" name="test" id="testB" autocomplete="off" value="B">B </label> </div> <div id="results"></div>

Just set up click events for the buttons and populate the results area in the event callback.只需为按钮设置click事件并在事件回调中填充结果区域。 No JQuery or Bootstrap is required.不需要 JQuery 或 Bootstrap。

But, radio buttons need to have their value attribute configured so that clicking one has meaning over the others.但是,单选按钮需要配置其value属性,以便单击一个按钮比其他按钮更有意义。 Also, autocomplete is irrelevant with radio buttons.此外, autocomplete与单选按钮无关。

 // Get a reference to the result area element let results = document.getElementById("results"); // Set up a click handler on the parent of the radio buttons so that // any clicks to the descendants will bubble up to the parent document.querySelector("div.btn-group").addEventListener("click", function(evt){ // Check if a radio button triggered the event if(evt.target.type === "radio"){ // Populate the results area with the value of the clicked element results.textContent = evt.target.value; } });
 <div class="btn-group" data-toggle="buttons"> <label id='RADIO' class="btn btn-primary"> <input type="radio" name="test" value="NONE">NONE </label> <label class="btn btn-primary"> <input type="radio" name="test" value="A">A </label> <label class="btn btn-primary"> <input type="radio" name="test" value="B">B </label> </div> <div id="results"></div>

Here, I added a button so you can get the status when clicked.在这里,我添加了一个按钮,以便您可以在单击时获得状态。 You should add a value attribute to your input fields.您应该向input字段添加value属性。

 $(document).ready(function(){ $(".detect").click(function(){ var radioValue = $("input[name='test']:checked").val(); if(radioValue){ $(".results").text(radioValue); } }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="btn-group" data-toggle="buttons"> <label id='RADIO' class="btn btn-primary"> <input type="radio" name="test" id="NONE" autocomplete="off">NONE </label> <label class="btn btn-primary"> <input type="radio" name="test" id="A" value="A" autocomplete="off">A </label> <label class="btn btn-primary"> <input type="radio" name="test" id="B" value="B" autocomplete="off">B </label> </div> <div class="results"></div> <button class="detect">Detect</button>

If you want to use the jQuery, you can take advantage of the delegate method.如果你想使用 jQuery,你可以利用委托方法。 https://api.jquery.com/delegate/#delegate-selector-eventType-handler https://api.jquery.com/delegate/#delegate-selector-eventType-handler

Using the delegate, you'll set just one handler that will deal with the value change inside the radio button group.使用委托,您将只设置一个处理程序来处理单选按钮组内的值更改。 This way the event will be handled even if the value inside the radio buttons group will change "programmatically", that is as a result of another code running on your website as long as that code will dispatch the "change" event.这样,即使单选按钮组内的值将“以编程方式”更改,也将处理该事件,这是由于在您的网站上运行的另一个代码,只要该代码将调度“更改”事件。

Remember to add the id on the radio buttons container and value attributes to the HTML structure (you will need the value attribute anyway on the server-side).请记住将单选按钮容器上的idvalue属性添加到 HTML 结构中(无论如何在服务器端都需要 value 属性)。

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script> $(document).ready(function() { $("#option").delegate("[type=radio]", "change", function(event) { $("#results").text( $(event.target).val() ); }); }); </script> <div id="option" class="btn-group" data-toggle="buttons"> <label id='RADIO' class="btn btn-primary"> <input type="radio" name="test" id="NONE" value="NONE" autocomplete="off">NONE </label> <label class="btn btn-primary"> <input type="radio" name="test" id="A" value="A" autocomplete="off">A </label> <label class="btn btn-primary"> <input type="radio" name="test" id="B" value="B" autocomplete="off">B </label> </div> <div id="results"></div>

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

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