如果我选择A,B,C输入框不是必填的。如果我选择C,测试输入框应该是必填的。 我试过了,但它不起作用。
HTML:
<select name="test" id="select" onclick="selection()">
<option value="">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="nil">nil</option>
</select>
<td>test</td>
<td><input type="text" name="test1" id="test1" />
<input type="button" value="submit" onclick="selection()" />
JS:
function selection()
{
var cat = document.getElementById('select').value;
if (cat == "nil") {
if (document.getElementById("test1").value == "") {
alert('Mandatory');
}
}
}
最佳答案
使用onchange选择事件。 Working jsFiddle here
HTML
<select name="test" id="select" onchange="selection()">
<option value="">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="nil">nil</option>
</select>
<td>test</td>
<td><input type="text" name="test1" id="test1" />
<input type="button" value="submit" onclick="selection()" />
Javascript
function selection()
{
var cat = document.getElementById('select').value;
if (cat == "nil") {
if (document.getElementById("test1").value == "") {
alert('Mandatory');
return false;
}
}
return true;
}
关于javascript - 根据下拉选择使输入字段成为必填项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26943428/