2014-01-14 46 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