我有JSP
,表格如下。我不希望任何与表单相关的按钮提交。
<form name="difficulty" action="${pageContext.request.contextPath}/game.do" method="post">
<div id="difficulty">
<label>Difficulty Level</label><br>
<select name="difficultyLevel" onselect="submitOption()">
<option value="easy" name="easy">Easy</option>
<option value="medium" name="medium">Medium</option>
<option value="hard" name="hard">Hard</option>
</select>
</div>
</form>
这是我的JS
:
function submitOption() {
document.getElementsByName('difficulty').submit();
}
选择该选项后,用户应该会看到servlet
的结果。如何解决这个问题?
答案 0 :(得分:1)
我测试了这个(简要地)并且在 onchange 回调中遇到了一些问题(没有发生)。然而,这很有用(使用Js注册事件,而不是HTML属性):
<form name="difficulty" action="${pageContext.request.contextPath}/game.do" method="post">
<div id="difficulty">
<label>Difficulty Level</label><br>
<select name="difficultyLevel">
<option value="easy" name="easy">Easy</option>
<option value="medium" name="medium">Medium</option>
<option value="hard" name="hard">Hard</option>
</select>
</div>
</form>
<script type="text/javascript">
document.getElementsByName("difficultyLevel")[0].onchange = function() {
document.getElementsByName('difficulty')[0].submit();
}
</script>
答案 1 :(得分:0)
document.getElementsByName()返回一个数组。这应该有效:
function submitOption() {
document.getElementsByName('difficulty')[0].submit();
}