单选按钮值返回未定义

时间:2019-05-30 13:08:46

标签: jquery html typescript radio-button undefined

按钮可见并且可以使用,但是当单击提交时,应该将选中的单选按钮的值(对于3组中的每一个)分配给变量,但是返回的值始终是不确定的。

由于一组按钮,我希望输出是值或包含值的对象

severityFormValue.on("click", (e) => {
  const loggedVia = $('input[name=loggedVia]:checked').val()
  const allStable = $('input[name = allStable]:checked').val()
  const EnvDown = $('input[name = EnvDown]:checked').val()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="dropdown">
       <button class="dropbtn" id="CCR_DACEbtn">CCR DACE ▼</button>
       <div id="drowdownCCR_DACEID" class="dropdown-content">
               Submitted by:
               <input type="radio" name="loggedVia" value="phone" checked> phone
               <input type="radio" name="loggedVia" value="email"> email<br>
               All services stable?
               <input type="radio" name="allStable" value="Yes"> Yes 
               <input type="radio" name="allStable" value="No" checked> No 
               <input type="radio" name="allStable" value="N/A"> N/A <br>
               Environment Down?
               <input type="radio" name="EnvDown" value="Yes"> Yes 
               <input type="radio" name="EnvDown" value="No" checked> No 
               <input type="radio" name="EnvDown" value="N/A"> N/A <br>
               <button type="button" id="severityFormSubmit" >Submit</button>
       </div>
   </div>`

1 个答案:

答案 0 :(得分:3)

第二组无线电输入的名称是AllStable,而不是allStable,因此选择器找不到它们,因为它区分大小写。修复该错误,您显示的代码可以正常运行:

var severityFormValue = $('#severityFormSubmit');

severityFormValue.on("click", (e) => {
  const loggedVia = $('input[name="LoggedVia"]:checked').val()
  const allStable = $('input[name="AllStable"]:checked').val();
  const envDown = $('input[name="EnvDown"]:checked').val()

  console.log(loggedVia);
  console.log(allStable);
  console.log(envDown);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dropdown">
  <button class="dropbtn" id="CCR_DACEbtn">CCR DACE ▼</button>
  <div id="drowdownCCR_DACEID" class="dropdown-content">
    Submitted by:
    <input type="radio" name="LoggedVia" value="phone" checked> phone
    <input type="radio" name="LoggedVia" value="email"> email<br> 
    
    All services stable?
    <input type="radio" name="AllStable" value="Yes"> Yes
    <input type="radio" name="AllStable" value="No" checked> No
    <input type="radio" name="AllStable" value="N/A"> N/A <br> 
    
    Environment Down?
    <input type="radio" name="EnvDown" value="Yes"> Yes
    <input type="radio" name="EnvDown" value="No" checked> No
    <input type="radio" name="EnvDown" value="N/A"> N/A <br>
    <button type="button" id="severityFormSubmit">Submit</button>
  </div>
</div>

还请注意,我将EnvDown变量名称更改为envDown,并将loggedVia name属性更改为LoggedVia,以保持与其对应值的一致性。