无法提交带有<选择多个必填项>在Internet Explorer中
Cannot submit form with <select multiple required> in Internet Explorer
首次提交时,<选择>被选中,导致HTML5验证失败,从而完全阻止提交。
然而,如果我们通过JavaScript选择一个选项,那么在随后的提交中提交仍然失败。
如果手动选择已选择选项以外的选项(或者取消选择并重新选择),则会成功。
Bug或功能?它是已知的行为还是在某个地方记录的?我找不到任何推荐人。
var done = false;
$(document).ready(function() {
$("#s").on("click", function() {
if (done) {
return;
}
// run once
done = true;
setTimeout(function() {
$("#a").prop("selected", true);
console.log($("#x").val());
}, 100); // delay to prevent submission with set option
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="get">
<select id="x" name="x" required="required" multiple="multiple">
<option value="a" id="a">Anton</option>
<option value="b">Berta</option>
</select>
<input id="s" type="submit" name="btn" value="Submit" />
</form>
这似乎与在<option>
上设置.prop
有关。
当您在select
输入上设置selectedIndex
时,一切正常。检查更新的小提琴:
document.getElementById('btn-select').addEventListener('click', function() {
var select = $("#x")[0];
// select the first one
// select.selectedIndex = 0;
// or loop through options and select based on a condition
for (var i = 0; i < select.options.length; i++) {
if (select.options[i].value === 'b') {
select.selectedIndex = i;
return;
}
}
})
https://jsfiddle.net/5hbey7en/8/
所以你的例子会变成:
var done = false;
$(document).ready(function() {
$("#s").on("click", function() {
if (done) {
return;
}
// run once
done = true;
setTimeout(function() {
$("#x")[0].selectedIndex = 0;
console.log($("#x").val());
}, 100); // delay to prevent submission with set option
});
});
相关文章:
- 在<页眉>标签
- 视频HTML没有'无法在Internet Explorer 11上工作
- 如何更改<svg>标记为<img>用js标记
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- Ajax文件加载和<输入>文件加载
- 为什么要使用0>javascript中的0
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 链接所有<a>Meteor
- 在<输入类型=“;文件“/>
- Div根据<选择>菜单
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- 如何首先设置样式<td>表中包含在窗体中的元素
- 通过点击<李>在jQuery中
- Internet Explorer缺少占位符支持,特别是密码字段
- 无法提交带有<选择多个必填项>在Internet Explorer中
- HTML5<音频>标记donesn'不能在Firefox和Internet Explorer上工作
- 使所有文本除<输入>在Internet Explorer中不可选择
- Internet Explorer筛选器<输入>透明的
- JQuery下拉菜单<李>链接在Internet Explorer中不起作用
- 它有任何实现HTML5<keygen>MS Internet Explorer中的功能