我有下面的脚本来验证文本字段。



< script src = "https://img.en25.com/i/livevalidation_standalone.compressed.js"
type = "text/javascript" >

< script type = "text/javascript" >
  var dom1 = document.querySelector('#form3471 #field1');
var field1 = new LiveValidation(dom1, {
  validMessage: "",
  onlyOnBlur: false,
  wait: 300
});

field1.add(Validate.Presence, {
  failureMessage: "This field is required"
});
< /script>

<form method="post" name="" action="https://s1788.t.eloqua.com/e/f2" onsubmit="setTimeout(function(){if(document.querySelector){var s=document.querySelector('form#form3471 input[type=submit]');if(s){s.disabled=true;}}},100);return true;" id="form3471"
class="elq-form">

  <label for="field1" style="display: block; line-height: 150%; padding: 1px 0pt 3px; float: left; width: 31%; margin: 0pt 15px 0pt 0pt; word-wrap: break-word">Name:*</label>
  <input id="field1" name="C_FirstName" type="text" value="" style="width: 46%">





我添加了一些单选按钮,并且也想验证单选按钮,如何修改它们以使其适用于单选按钮?



<label for="field0" style="display: block; line-height: 150%; padding: 1px 0pt 3px; white-space: nowrap">Country*</label>

<label style="display: block; float: left; padding-right: 10px; padding-left: 22px; text-indent: -22px">
  <input name="radioButtons" type="radio" value="country1" style="vertical-align: middle; margin-right: 7px">
  <span style="vertical-align: middle">Country1</span>
</label>

<label style="display: block; float: left; padding-right: 10px; padding-left: 22px; text-indent: -22px">
  <input name="radioButtons" type="radio" value="country2" style="vertical-align: middle; margin-right: 7px">
  <span style="vertical-align: middle">Country2</span>
</label>

最佳答案

为您的单选按钮执行功能。现在,您没有此功能。

像这样:

function myFunction() {
    var answer = document.getElementById("radioBtn").checked;


  document.getElementById("value").innerHTML = answer;
}


http://codepen.io/anon/pen/oBPpZz

关于javascript - 验证是否选中了单选按钮?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42107922/

10-12 00:07
查看更多