2013-03-24 47 views
0

當組合框的選擇更改時,我希望將特定的數組項設置爲輸入標籤的值。在更改組合框時在輸入框中插入數組項目

<%@ page language="java" import="java.util.*;" pageEncoding="ISO-8859-1"%> 

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
     <head> 
     </head> 
    <script language="javascript"> 

    function check() 
     { 
      if(!validate()) 
       return false; 
     } 

    function validate() 
     { 
      if (document.form.amount.value <=0) 
      { 
       alert ("Please enter valid loan amount."); 
       return false; 
      } 
      else 
       return true; 
     } 

     var arr = new Array(); 
     arr[0] = new Array("-select-", "10.0","10.5", "12.0"); 

     function change(combo1) { 
      document.getElementById("rate").style.visibility = "visible"; 
      var sel = document.getElementById("combo1"); 
     var val = document.getElementById("rate").value; 
     for(var i = 0, j = sel.options.length; i < j; ++i) { 
      if(sel.options[i].innerHTML == val) { 
       sel.selectedIndex = i; 
       break; 
      } 
     } 
    } 

} 
    </script> 
     <jsp:include page="include.jsp"></jsp:include> 

     <body> 
      <br> 
      <form action="PaymentServlet.do" name="form" method="post" 
       ONSUBMIT="return check()"> 
       <fieldset> 
        <legend> 
         <font style="font-family: sans-serif" size="4px" color="Megenta">Online Loan 
          Application Page</font> 
        </legend> 
        <br /> 

        Loan Type 
        <select name="combo1" onchange="change(this);"> 
         <option value="0"> 
          -Select- 
         </option> 
         <option value="1"> 
          Home Loan 
         </option> 
         <option value="2"> 
          Education Loan 
         </option> 
         <option value="3"> 
          Car Loan 
         </option> 
        </select> 
        <br /> 

        Interest Rate(%)<input type="text" name="rate" style="visibility: hidden;"> 
        <br /> 
        Loan Amount 
        <input type="text" name="loanamount" /> 
        <br /> 
        <input type="submit" name="go" value="o Go o" /> 
       </fieldset> 
      </form> 
     </body> 
    </html> 

取決於貸款型我想選擇來自陣列的相應利率和希望此數組項將被用作輸入的標記,其名稱=「速率」的值。

+0

和你有什麼問題? – 2013-03-24 19:22:23

+0

user @ mr.VVoo無法獲取輸入標籤值中的數組值。 – rahul 2013-03-24 19:24:12

回答

0

爲了得到一個組合框的值,你需要做到以下幾點:

var val = sel.options[sel.selectedIndex].value; 

sel是您的組合框

0

不能給予名稱paramater使用document.getElementById("elementid")。參數必須是元素名稱的id

實際上,您不需要使用document.getElementById,因爲您將this作爲參數更改函數返回。 this指的是實際的元素。

所以,你的變化函數應該是這樣的,

function change(combo1) { 
     document.getElementById("rate").style.visibility = "visible"; 
     document.getElementById("rate").value = arr[combo1.selectedIndex]; 
} 

你的數組定義是錯誤的爲好。正確的是在這裏,

var arr = new Array(); 
arr[0] = new Array("-select-", "10.0","10.5", "12.0"); 

var arr = new Array("-select-", "10.0","10.5", "12.0"); 

最後不要忘記加上id屬性的速率輸入,

<input type="text" id="rate" name="rate" style="visibility: hidden;"> 

這裏是完整的代碼和一個DEMO

<script language="javascript"> 

function check() 
    { 
     if(!validate()) 
      return false; 
    } 

function validate() 
    { 
     if (document.form.amount.value <=0) 
     { 
      alert ("Please enter valid loan amount."); 
      return false; 
     } 
     else 
      return true; 
    } 

    var arr = new Array("-select-", "10.0","10.5", "12.0"); 

    function change(combo1) { 
     document.getElementById("rate").style.visibility = "visible"; 
     document.getElementById("rate").value = arr[combo1.selectedIndex]; 
} 

</script> 
    <body> 
     <br> 
     <form action="PaymentServlet.do" name="form" method="post" 
      ONSUBMIT="return check()"> 
      <fieldset> 
       <legend> 
        <font style="font-family: sans-serif" size="4px" color="Megenta">Online Loan 
         Application Page</font> 
       </legend> 
       <br /> 

       Loan Type 
       <select name="combo1" onchange="change(this);"> 
        <option value="0"> 
         -Select- 
        </option> 
        <option value="1"> 
         Home Loan 
        </option> 
        <option value="2"> 
         Education Loan 
        </option> 
        <option value="3"> 
         Car Loan 
        </option> 
       </select> 
       <br /> 

       Interest Rate(%)<input type="text" id="rate" name="rate" style="visibility: hidden;"> 
       <br /> 
       Loan Amount 
       <input type="text" name="loanamount" /> 
       <br /> 
       <input type="submit" name="go" value="o Go o" /> 
      </fieldset> 
     </form> 
    </body> 
0

如果你的數組定義是像

arr[0] = new Array("-select-", "10.0","10.5", "12.0"); 

然後

function change(combo1) { 
    document.getElementById("rate").style.visibility = "visible"; 
    var rate = document.getElementById("rate"); 
    rate.value = arr[0][combo1.selectedIndex]; 
}