2014-01-14 44 views
0

我有一个form多个下拉菜单和一个submit按钮。根据从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页:

<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> 

一个提交按钮后击中,它调用servlet来提供基于这些选择的响应,从而刷新了整个test.jsp如何使禁用值持久 ,直到用户不单独更改选项。

请纠正我,如果我对任何错误和我需要添加它有持久性。

回答

0

在禁用下拉列表时,可以在Javascript中设置cookie,在页面加载时读取该cookie并在Javascript中再次禁用。或者将禁用功能移入JSP并使用会话。

要禁用JSP,你可以做这样的事情:

<% 
boolean blnOneIsDisabled = //get from session 
String oneIsDisabled = (!blnOneIsDisabled) ? "" : "disabled='true'"; 
%> 
<select id="type" name="selectionOne" <%=oneIsDisabled%> > 
+0

谢谢developerwjk,所以目前我在我的JSP文件都被processign响应一个Servlet会从被张贴在发送请求请给我举个例子或链接,通过这些例子或链接我可以学习如何进行会话nd同样继续,因为我是JSP新手,所以试图把所有的东西都粘在一起。 – AKIWEB

+0

会话就像使用session.setAttribute(name,value)一样简单,并检索session.getAttribute(name);当检索时,它会返回一个Object,所以你必须进行类型转换:boolean x =(boolean)session.getAttribute(name);例如。 – developerwjk

+0

谢谢developerwjk,我确实遵循了一些简单的例子,它们让我可以将任何表单元素的值保存到'session变量'中。但在我的情况下,该特定的下拉值或文本的值是持久的,但不是JQuery脚本部分。您如何通过使用'session'来指导实现这一点? – AKIWEB

0

我有点糊涂了,你与提交按钮做什么,但你可以尝试使用jQuery用。对(“提交” )的preventDefault()方法应该刷新页面停止。

$(function() { 
    $('form').on('submit', function (e) { 
    //code to run when submitting 
     }); 
     e.preventDefault(); 
+0

谢谢Austen,我基本上有一个表单,它会调用servlet来响应我的JSP页面。因此,只要我选择了值并点击提交,它就会给出结果并且由于刷新而返回到其先前的状态,并且禁用的功能被启用。希望我回答?我们的怀疑 – AKIWEB