繁体   English   中英

验证单选按钮是否被选中?

[英]Validate if radio button was checked?

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

 < 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

请试试这个

if ($('input[name=radioButtons]:checked').val() == 'country1' ) { 
    //validation
}

暂无
暂无

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

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