Text field appearing with along radio buttons, where my form does not have a text input

时间:2016-05-05 09:11:24

标签: javascript html

enter image description here I get this when i run my code. I have no text inputs but I am seeing text inputs along with radio buttons when I run this.

<script>

function validateForm() {
    var x = document.forms["myForm"][0].value;
    if (x == null || x == "") {
        alert("i must be checked");
        return false;
    }
 var y = document.forms["myForm"][1].value;
    if (y == null || y == "") {
        alert("j must be checked");
        return false;
    }
    var z = document.forms["myForm"][2].value;
    if (z == null || z == "") {
        alert("k must be checked");
        return false;
    }
}
</script>

<body>
<form name="myForm" onsubmit="return validateForm()">
<input type="radio" name="i">click<input/>
<input type="radio"name="j">click<input/>
<input type="radio"name="k">click<input/>
<input type="submit" value="submit">
</form>
</body> 

3 个答案:

答案 0 :(得分:1)

您在每个收音机的末尾使用这些空输入标签,输入标签默认呈现为文本框

<input/>

此外,您必须使用结束标记关闭无线电

<input type="radio" name="i" />click

答案 1 :(得分:0)

输入标记中不应包含任何值。

使用标签,如下所示:

<input type="radio" name="i" id="i1" />
<label for="i1">click</label>

答案 2 :(得分:0)

每行的第二个input标记不正确;每次你有 <input type="radio"name="k">click<input/>然后浏览器将其解释为

<input type="radio"name="k" /> (automatically adds self-closing slash)
click
<input type="text" /> (gives the closing input tag type="text" by default)

要解决此问题,您的输入应如下所示:

<input type="radio"name="k" /> click

(注意无线电输入标签内的斜线)