根据下拉选择将输入字段设置为必填字段
Make input field mandatory based on drop down selection
如果我选择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 事件进行选择。在这里工作 jsFiddle
.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()" />
爪哇语
function selection()
{
var cat = document.getElementById('select').value;
if (cat == "nil") {
if (document.getElementById("test1").value == "") {
alert('Mandatory');
return false;
}
}
return true;
}
在这里工作演示
你应该
使用onchange
而不是onclick
,if (cat == "C")
而不是if(cat == "nil")
function selection()
{
var cat = document.getElementById('select').value;
if (cat == "C") {
if (document.getElementById("test1").value == "") {
alert('Mandatory');
}
}
}
<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" onchange="selection()" />
<input type="button" value="submit" onclick="selection()" />
此外,您需要使用 input onchange="selection()"
来检测用户在选择输入时是否已清除输入c
。
<html>
<head>
<title></title>
</head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>
<body>
<script>
$(document).delegate("#select","change",function(e){
if ($(this).val() === 'C')
$('#save').attr('onclick','chk()');
else
$('#save').removeAttr('onclick');
});
function chk()
{
var name=$('#name').val();
if(name=="")
{
alert("Mr. Hussy Please fill this box");
}
}
</script>
<select name="test" id="select">
<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="name" />
<input id='save' type="button" value="submit" />
</body>
</html>
相关文章:
- 如何将Date字段设置为等于另一个Date+Int值,该值表示月份值
- Sencha触摸,字段设置为Change Listener
- 将字段设置为有条件地使用所需的字段验证器
- crm 2011 javascript将日期字段设置为null
- 如何在自定义指令中将字段设置为$dirty
- javascript函数将上传文件字段设置为deafult
- 根据角度中的另一个字段设置输入字段的默认值
- Yii2 输入字段设置禁用取决于其他字段
- JavaScript for HTML5 必填字段设置为在 Safari 中工作
- 在 podio 中将计算日期字段设置为 null(因此使用 javascript)
- MongoDB:确保只有一个文档可以将字段设置为 true
- HTML 输入字段设置范围
- 如何使用 eval() 为作为参数提供的字段设置值
- j查询如何将值从输入字段设置为其他元素数据属性
- 使用具有金钱掩码的字段设置动态最大值
- 将输入字段设置为包含 window.location.href
- 根据下拉选择将输入字段设置为必填字段
- 从输入字段设置变量
- 从html输入字段设置javascript变量
- 如何在angularjs 1.3中将表单中的特定字段设置为dirty