简体   繁体   English

提交选中的单选按钮的输入值

[英]Submit input value for checked radio button

I made a hide function for my radio button. 我为单选按钮设置了隐藏功能。 It will show/hide a div whenever user clicked the radio button. 每当用户单击单选按钮时,它将显示/隐藏div。 Javascript: 使用Javascript:

      $('input[type="radio"]').click(function () {
                if ($(this).attr("id") == "CodeYes") {
                    $("#time").show();
                }
                if ($(this).attr("id") == "CodeNo") {
                    $("#time").hide();
                }
      });

HTML form: HTML形式:

   <div class="form-group">
     <label class="col-sm-5 control-label">Code Initiated </label>
      <div class="col-sm-7">
      <label class="radio radio-inline">
      <input type="radio" name="CodeInitiated" value="true" id="CodeYes"> Yes</label>
      <label class="radio radio-inline">
<input type="radio" name="CodeInitiated" value="false" id="CodeNo"> No</label>
     </div>
    </div>

<div class="form-group" id="time">
 <label class="col-sm-5 control-label">Time </label>
  <div class="col-sm-3 ">
  <div Class="input-group ">
   <input Class="form-control input-sm" id="CodeInitiatedTime" name="CodeInitiatedTime" type="datetime-local" />
  </div>
 </div>
</div>

when the user click at yes button, time div will show up. 当用户单击“ yes按钮时,将显示time div。 the problem is, when the user click at yes button and type an input in the input box inside, then the user click at no button which means, he want to cancel out the previous input. 问题是,当用户单击“ yes按钮并在其中的输入框中键入输入时,然后用户单击“ no按钮,这意味着他要取消以前的输入。 when the form is submitted, the input still holding the value and bring it to the controller. 提交表单后,输入仍保留该值并将其带到控制器。 how to make sure the value is only be sumbitted if radio button clicked at the correct button? 如何确保仅在单选按钮上单击正确按钮后才对值求和?

When you are hiding the div , you can also disable the input (and vice-versa) 隐藏div ,也可以disable input (反之亦然)

  $('input[type="radio"]').click(function () {
            if ($(this).attr("id") == "CodeYes") {
                $("#time").show();
                $("#time").find(":input").prop( "disabled", false ); //enable it back
            }
            if ($(this).attr("id") == "CodeNo") {
                $("#time").hide();
                $("#time").find(":input").prop( "disabled", true ); //disable on hide
            }
  });

Disabled inputs are not submitted. 禁用的输入未提交。

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

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