如何验证javascript中的下拉列表和radiobutton是用户的强制选择,如果用户没有选择则抛出警报 [英] how do I validate dropdown list and radiobutton in javascript to be a mandatory selection by user and throw an alert if user doesn't select
本文介绍了如何验证javascript中的下拉列表和radiobutton是用户的强制选择,如果用户没有选择则抛出警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个下拉列表和一些用户在表单上选择的单选按钮。
提交表单必须发出警告说请从dropdown / radiobutton中选择一个值 。
有谁能告诉我如何使用javascript验证来实现这个目标?
我尝试使用必需属性进行下拉但我不知道如何让它显示出来如果没有选择则发出警报..
解决方案
尝试从这个simle代码中获取:
< html >
< head >
< title > 无标题< / title >
< script type = text / javascript >
function checkit(){
if(!document.getElementById(cid)。checked){
alert(勾选复选框)
}
else {
a.submit();
}
}
< / script >
< / head >
< body < span class =code-keyword>> < form id = form1 >
< input type = 复选框 id = cid / >
< input type = 按钮 value = 提交 < span class =code-attribute> onclick = javascript:checkit(); / >
< / form >
< / body >
< / html >
I have a dropdown list and some radio buttons to be selected on a form by the user.
The form on submission must throw an alert saying,"please select a value from dropdown/radiobutton".
Can anyone please tell me how I can achieve this using javascript validation?
I tried using required attribute for dropdown but I am not aware how I can make it show an alert if not selected..
解决方案
try to get from this simle code :
<html> <head> <title>Untitled</title> <script type="text/javascript"> function checkit(){ if(!document.getElementById("cid").checked){ alert("Check the checkbox") } else{ a.submit(); } } </script> </head> <body><form id="form1"> <input type="checkbox" id="cid"/> <input type="button" value="Submit" onclick="javascript:checkit();"/> </form> </body> </html>
这篇关于如何验证javascript中的下拉列表和radiobutton是用户的强制选择,如果用户没有选择则抛出警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文