2011-11-08 45 views
1

我有disabled=true文本框和combox框在我的jsp中。獲取禁用真正的數據從jsp到struts2動作類

當我嘗試將這些值映射回行動時,它們消失。

我不想再次調用DB。如何設置disabled=truetextboxescombo boxes的數據值爲hidden values

提前感謝。

+0

我想限制用戶輸入某些情況下的值。 – kitokid

+0

我找到了解決方案。我只是使用if條件並在jsp中設置readonly =「readonly」。這好多了。 :) – kitokid

回答

4

禁用元素值的屬性未隨表單一起提交,這對struts2不是問題,而是HTML行爲。要處理此行爲,請使用以下實現:

  1. 使用readonly =「readonly」屬性可防止修改而不是使用禁用。 (這將不提供少量元素)
  2. 使用onfocus =「返回false」來防止對html元素的任何焦點。這將阻止其價值的修改。您可以使用CSS來使這些元素看起來像只讀或禁用。
  3. 在禁用元素之前,請創建一個隱藏元素並附上您即將禁用的元素的值。這將確保項目被提交。

請參閱下面的select元素的實現。您可以使用s:id的id屬性來設置select元素的html id。

<select id="demoSelect" class="readonly"> 
    <option value="0">A</option> 
    <option value="1">B</option> 
    <option value="2" selected="selected">C</option> 
    <option value="3">D</option> 
    <option value="4">E</option> 
    <option value="5">F</option> 
</select> 
<input type="hidden" value="2" name="demoSelectDefault"/> 

的jQuery:

$(document).ready(

    function() { 
     $("select.readonly").live('change', function() { //live() makes sure that this is executed if you apply the class to the element even after the initial load. So, if you set the readonly class to a select element, you are done. 
      var selectElement = this;  
      $("input[type=hidden][name=" + this.id + "Default]").each(//This is implemented in case of multiple select support. You will need to select nothing at first and then make this select each of this element 
       function() { 
        selectElement.value = this.value; 
       } 
      ); 

     }); 
    } 

); 

在這裏,當你實現這個用struts,填充S:選擇,然後使用S:隱藏元素生成對應的默認值。

+0

像你說的,我很難設置readonly屬性來選擇struts2的標籤。 :( – kitokid

+0

看看改變的答案是否可以幫到你 –

+0

我會檢查出來的,謝謝。:) – kitokid