当我在HTML中选择特定选项时,要消失的输入字段
Input field to disappear when I select a a specific option in HTML
我有一个html格式的表单,我在这里发布了一个示例:
<html>
<head></head>
<body>
<form>
<p> <select name="job">
<option type="text" selected disabled>Select Role</option>
<option type="text" value="administrator">Administrator</option>
<option type="text" value="manager">Manager</option>
<option type="text" value="Candidate">Candidate</option>
</select>
</p>
<p><input type="file"></p>
<p><input type="submit" value="Create Account"></p>
</form>
</body>
</html>
选择Administrator
时,如何使file
字段消失?我已经尝试过禁用它,但这并不是我想要做的。
你可以做:
$('select').on('change', function() {
var types = ['manager', 'administrator'];
if (types.indexOf(this.value) >= 0)
$(':file').hide();
else
$(':file').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<p>
<select name="job">
<option type="text" selected disabled>Select Role</option>
<option type="text" value="administrator">Administrator</option>
<option type="text" value="manager">Manager</option>
<option type="text" value="Candidate">Candidate</option>
</select>
</p>
<p>
<input type="file">
</p>
<p>
<input type="submit" value="Create Account">
</p>
</form>
在select标签中添加一个更改时侦听器
<select id="job" name="job" onchange="jsFunction()">
<option type="text" selected disabled>Select Role</option>
<option type="text" value="administrator">Administrator</option>
<option type="text" value="manager">Manager</option>
<option type="text" value="Candidate">Candidate</option>
</select>
如果你想在js中不使用jquery:
function jsFunction(){
var myselect = document.getElementById("job");
var lvl = ['manager', 'administrator'];
if (lvl.indexOf(myselect.options[myselect.selectedIndex].value) >-1 )
{
myselect.style.display = 'none';
}
else
{
myselect.style.display = 'inline';
}
}
试试这个:
var user = job.options[job.selectedIndex].value;
if(user === 'Administrator' || user === 'Manager') {
file.outerHTML = "";
}
HTML:
<form>
<p> <select id="job">
<option type="text" selected disabled>Select Role</option>
<option type="text" value="administrator">Administrator</option>
<option type="text" value="manager">Manager</option>
<option type="text" value="Candidate">Candidate</option>
</select>
</p>
<p><input id="file" type="file"></p>
<p><input type="submit" value="Create Account"></p>
</form>
您也可以使用此替代方案:
$(document).ready(function() {
$("select").change(function() {
if ($('select option:selected').val() == "manager" || $('select option:selected').val() == "administrator") {
$('input[type=file]').hide(); //hides the file input
} else {
$('input[type=file]').show(); //shows the file input if not manager or administrator
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form>
<p>
<select name="job">
<option type="text" selected disabled>Select Role</option>
<option type="text" value="administrator">Administrator</option>
<option type="text" value="manager">Manager</option>
<option type="text" value="Candidate">Candidate</option>
</select>
</p>
<p>
<input type="file">
</p>
<p>
<input type="submit" value="Create Account">
</p>
</form>
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- 在wordpress一定时间后更改自定义字段
- 将phonegap图像发布到php/mysql-图像字段从发布请求中消失
- 当我在HTML中选择特定选项时,要消失的输入字段
- 隐藏/禁用字段从 req.body 中消失(Express JS bodyParser)
- 更改焦点后,输入字段中的文本将消失
- 输入字段html5占位符没有'不要在javascript中添加值后就消失
- 动态添加的输入字段立即消失
- 输入字段逐渐消失,取代了网站的其他部分
- jQuery Validator无效输入字段在成功提交后消失
- ExtJs文件字段在第一次使用后消失
- 使用Angular.js点击输入类型文件字段时,图像名称会消失
- mm菜单&prototype.js冲突,html在使用搜索字段时消失
- 使用javascript和html使字段出现/消失
- 用户单击输入字段,值将消失