2011-04-15 55 views
2

請幫助我,我這裏有一些代碼:如何創建文本框在jQuery的selextbox價值變動股利

<html> 
    <head> 
     <title>Select Example</title> 
     <script type="text/javascript" src="/admin_template/jquery.min.js"></script> 
     <script type="text/javascript"> 
     $(document).ready(function(){ 

      $("#developer").change(onSelectChange); 

     }); 

     function onSelectChange(){ 
      var selected = $("#developer option:selected"); 
      var output = ""; 
      if(selected.val() != 0){ 
       output = "<input style="width:120px" type="text" name="other_drcname" value='"+ selected.text()+"'/>"; 
      } 
      $("#output").html(output); 
     } 
     </script> 
    </head> 

    <body> 

     <h3>Developers</h3> 

     <select id="developer"> 
      <option value="0">Select Developer</option> 
      <option value="1">Todd Sharp</option> 
      <option value="2">Brian Meloche</option> 
      <option value="3">Ray Camden</option> 
      <option value="4">Sean Corfield</option> 

      <option value="5">Ben Nadel</option> 
      <option value="6">Mark Drew</option> 
      <option value="7">Rey Bango</option> 
      <option value="8">Mark Mandel</option> 
      <option value="9">Joe Rinehart</option> 
      <option value="10">Dan Vega</option> 
     </select> 
     <br /><br /> 


     <div id="output"> 

     </div> 

    </body> 
    </html> 

我想在選擇框的值變化<div id="output">創建文本框

回答

1

看起來你可能對這條線有問題:

output = "<input style="width:120px" type="text" name="other_drcname" value='"+ selected.text()+"'/>"; 

嘗試將其更改爲:

output = "<input style=\"width:120px\" type=\"text\" name=\"other_drcname\" value='"+ selected.text()+"'/>"; 

你必須轉義引號或者使用單引號(撇號)

+0

由於它的工作原理 – Sardor 2011-04-15 16:03:31

1

output = "<input style="width:120px" type="text" name="other_drcname" value='"+ selected.text()+"'/>";是錯誤的。你引用它是錯誤的。試試這個:

output = "<input style='width:120px' type='text' name='other_drcname' value='"+ selected.text()+"'/>";

1

此行

output = "<input style="width:120px" type="text" name="other_drcname" value='"+ selected.text()+"'/>"; 

應該

output = "<input style='width:120px' type='text' name='other_drcname' value='"+ selected.text()+"'/>"; 
1

或者你可以這樣做:

輸出=「<輸入風格=」寬度:120px'type ='text'name ='other_drcname'value ='「+ selecte d.text()+ 「'/>」;

只要確保不混淆單引號和雙引號(以下簡稱「<」後,擺脫了空間的)