简体   繁体   English

表单提交时无法选择单选按钮

[英]Can't get select radio button on form submit

I'm having problems getting the radio button value when the form is submitted. 提交表单时,我在获取单选按钮值时遇到问题。 I thought that when the form is submitted it would get the final selection that user made but it appears it doesn't recognize which one is selected. 我以为,提交表单后,它将获得用户做出的最终选择,但似乎无法识别选择了哪一个。

On initial load: This is inside my form 初始加载时:这在我的表单中

<div id="question1">
  <label> Do you shop mostly on-line or at physical stores? </label>
    <div class="radioButtons">
      <span>
       Mostly on-line stores
       <input name="question1" value="on-line" type="radio">
      </span>
      <span>
        mostly physical stores
        <input name="question1" value="physical" type="radio">
      </span>
      <span>
        About both equaly
        <input name="question1" value="both" type="radio">
      </span>
   </div>
</div>

in my submit handler 在我的提交处理程序中

    $(".aboutmeQuestions").on("submit", function(e){
        e.preventDefault();
        var values = {};
        var $inputs = $(".aboutmeQuestions :input").not(":submit");

        $inputs.each(function(){
            values[this.name] = {answer : $(this).val()};
        });
        //I thought the above would get the radio buttons but I think it got the last choice as the selected one no matter what was chosen. so I tried below because I thought that the checked attribute would change automatically.
        $("input[type='radio']").each(function(){
            console.log("this ",this);
            if($(this).attr("checked")){
                values[this.name] = {answer : $(this).val()};
            }

        })

In my EJS: 在我的EJS中:

    <div id="question1">
           <label> <%=aboutUser.question1.question%> </label>
           <div class = "radioButtons">
                <span>Mostly on-line stores <input type="radio" name="<%=aboutUser.question1.name%>" value="on-line" <% if(aboutUser.question1.answer == "on-line"){%> checked  <%}%>> </span>
                <span>mostly physical stores <input type="radio" name="<%=aboutUser.question1.name%>" value="physical" <% if(aboutUser.question1.answer == "physical"){%> checked <%}%>> </span>
                <span> About both equaly <input type="radio" name="<%=aboutUser.question1.name%>" value="both" <% if(aboutUser.question1.answer == "both"){%> checked  <%}%> ></span>
            </div>

    </div>

I tested it out. 我测试了一下。 After clicking on nothing and pressing the submit button I get for values object, question5[answer]: maybe" question1[answer]: "both" that should of been equal to nothing. It is always like that no matter what I click. I don't like that pleas help me fix it. I didn't think I have to use a change method. 单击任何内容并单击提交按钮后,我得到values对象,即question5[answer]: maybe" question1[answer]: "both"应该等于空”。无论单击什么,总是这样。不喜欢那个请求可以帮助我解决它,我认为我不必使用更改方法。

Ps I save this data to DB and the inputs should be populated with answers so when I reload the page <span> About both equaly<input name="question1" value="both" checked="" type="radio"> </span> . Ps我将此数据保存到DB,并且输入中应包含答案,因此,当我重新加载页面时<span> About both equaly<input name="question1" value="both" checked="" type="radio"> </span> it shouldn't have been checked since I didn't select anything. 由于我没有选择任何内容,因此不应该对其进行检查。

First, remove the checked attribute from all the checkboxes unless there is ONE that you want to be pre-selected and in that case add it to just that ONE. 首先,从所有复选框中删除checked属性,除非要预选一个,然后将其添加到该复选框中。

There is no need to loop through the radio buttons to see which was checked. 无需遍历单选按钮即可查看已选中的按钮。 Just use the :checked pseudo-class. 只需使用:checked伪类。 Change this: 更改此:

    $("input[type='radio']").each(function(){
        console.log("this ",this);
        if($(this).attr("checked")){
            values[this.name] = {answer : $(this).val()};
        }

    })

to this: 对此:

   var checkedRad = $("input[type='radio']:checked");
   values[this.name] = {answer : checkRad.value};

or, if you don't need a reference to the checked radio button after getting its value:: 或者,如果在获取其值后不需要引用选中的单选按钮,则:

   values[this.name] = {answer : $("input[type='radio']:checked")};

 $("input[type=radio]").on("click", function(e){ console.log($("input[type='radio']:checked").val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div id="question1"> <label> Do you shop mostly on-line or at physical stores? </label> <div class="radioButtons"> <span> Mostly on-line stores <input name="question1" value="on-line" type="radio"> </span> <span> mostly physical stores <input name="question1" value="physical" type="radio"> </span> <span> About both equaly <input name="question1" value="both" type="radio"> </span> </div> </div> 

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

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