我有一个form
有多个下拉菜单和一个submit
按钮。根据{{1}}做出的任何选择,我会停用其他下拉菜单,但在点击提交按钮后,它会刷新页面,并DropDown
禁用下拉菜单到set
。即使在页面刷新后,如何禁用这些下拉列表。
以下是我的enabled
:
Script
我的<script>
//Script to disable the dropdown on any selection
var pickId;
$(document).ready(function () {
$('select').change(function () {
$this = $(this);
pickId= $this.attr('id');
pickValue = $this.val();
if (pickId== 'type') {
$('select').prop('disabled', false);
if (pickValue == 'one') {
$('#parts').prop('disabled', true);
} else if (pickValue == 'two') {
$('#country').prop('disabled', true);
} else if (pickValue == 'three') {
$('#parts').prop('disabled', true);
$('#country').prop('disabled', true);
$('#today').prop('disabled', true);
}
}
})
});
</script>
页面:
JSP
点击提交按钮后,它调用servlet根据这些选择提供响应,从而刷新整个<div id='first'>
<form id='form' method='post' action='/test/'>
<input type="hidden" name="action" value="checker">
<select id="type" name="selectionOne">
<option value="main">selection1</option>
<option value="one">Selection2</option>
<option value="Two">Selection3</option>
<option value="Three">Selection3</option>
</select>
<% // tried to do in JSP but did not work???
String typeChker = request.getParameter("selectionOne");
%>
<script> // tried to do in JSP but did not work???
document.getElementById("type").value = '<% out.print(typeChker); %>';
</script>
<select id="Parts" name="partially">
<option value="1">1</option>
<option value="12">12</option>
</select>
<%
String dur = request.getParameter("partially");
%>
<script>
document.getElementById("Parts").value = '<% out.print(dur); %>';
var e = document.getElementById("Parts");
var strUser = e.options[e.selectedIndex].text;
</script>
<select id="country" name="ally">
<option value="6">USA</option>
<option value="9">UK</option>
</select>
<select id="today" name="aty">
<option value="123">Today</option>
<option value="6">tomorrow</option>
</select>
<input type="Submit" Value="Submit">
</form></div>
如何使禁用值持久化
直到用户不自行更改选项。
如果我发现任何错误,请纠正我,为了让它持续存在,我需要添加什么。
答案 0 :(得分:0)
在禁用下拉列表时在Javascript中设置cookie,在页面加载时读取该cookie并在Javascript中再次禁用。或者将禁用移动到JSP中并使用会话。
要在JSP中禁用,您可以执行以下操作:
<%
boolean blnOneIsDisabled = //get from session
String oneIsDisabled = (!blnOneIsDisabled) ? "" : "disabled='true'";
%>
<select id="type" name="selectionOne" <%=oneIsDisabled%> >
答案 1 :(得分:0)
我对使用提交按钮做了什么感到困惑,但你可以尝试使用带有.on('submit')的jquery。使用preventDefault()可以阻止它刷新页面。
$(function () {
$('form').on('submit', function (e) {
//code to run when submitting
});
e.preventDefault();