[英]javascript radio button validate
hey guys i just try to do some validate for my javascript radio button, but is not work. 大家好,我只是尝试对我的JavaScript单选按钮进行一些验证,但无法正常工作。 every time i click submit button it just nothing happened.below is the code.
每次我单击提交按钮,它什么都没有发生。下面是代码。
HTML part HTML部分
<form name="FirstPizza">
<td>
Style:
<br/>
<input type="radio" name="style"/>
Thin Crust
<br />
<input type="radio" name="style" />
Deep Dish
<br />
<input type="radio" name="style" />
Sicilian
</td>
</form>
Javascript: 使用Javascript:
function submitForm(){
if((!form.style[0].checked) && (!form.style[1].checked) && (!form.style[2].checked))
{
window.alert("You must have a style")
}
i should come with a box but nothing happen. 我应该带一个盒子,但是什么也没发生。 Do i miss something?
我想念什么吗?
You were close! 你近了! According to your comment, your
form
variable was not defined. 根据您的评论,您的
form
变量未定义。 You need to define form
somewhere like this: 您需要在这样的地方定义
form
:
function submitForm() {
// define the form variable
var form = document.getElementsByName("FirstPizza")[0];
if((!form.style[0].checked) && (!form.style[1].checked) && (!form.style[2].checked)) {
window.alert("You must have a style");
} else {
window.alert("a style was selected");
}
}
Use IDs for your form controls! 为表单控件使用ID! Try this:
尝试这个:
HTML part HTML部分
<form name="FirstPizza">
<td>
Style:
<br/>
<input type="radio" id="rbThinCrust" name="style"/>
Thin Crust
<br />
<input type="radio" id="rbDeepDish" name="style" />
Deep Dish
<br />
<input type="radio" id="rbSicilian" name="style" />
Sicilian
</td>
</form>
Javascript: 使用Javascript:
function submitForm(){
if (!document.getElementById("rbThinCrust").checked && !document.getElementById("rbThinCrust").checked && !document.getElementById("rbThinCrust").checked)
//Logic for nothing being checked
}
Both your HTML and JavaScript are completely broken. 您的HTML和JavaScript都已完全损坏。
First, your HTML. 首先,您的HTML。 You have no HTML for a button there, which you need should you wire your javascript event to it in the first place.
您那里没有用于按钮的HTML,如果您首先将javascript事件连接到该按钮,则需要此按钮。
You would create a submit button like this: 您将创建一个提交按钮,如下所示:
<input type="submit" value="Submit" onclick="submitForm(this)" />
Your radio buttons also should have their text set via the value
attribute, like so: 您的单选按钮还应该通过
value
属性设置其文本,如下所示:
<input type="radio" name="style" value="Thin Crust" />
<input type="radio" name="style" value="Deep Dish" />
<input type="radio" name="style" value="Sicilian" />
Now your javascript, it should look something more like this: 现在,您的JavaScript看起来应该像这样:
function submitForm(form){
if((!form.style[0].checked) && (!form.style[1].checked) && (!form.style[2].checked))
{
window.alert("You must have a style")
}
Hope that works for you. 希望对您有用。
And your end HTML should look like this: 您的最终HTML应该如下所示:
<form name="FirstPizza">
Style:<br />
<input type="radio" name="style" value="Thin Crust" />Thin Crust<br />
<input type="radio" name="style" value="Deep Dish" />Deep Dish<br />
<input type="radio" name="style" value="Sicilian" />Sicilian
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.