2010-01-24 23 views
1

我遇到了此工作表單的問題。無論何時添加或刷新頁面,值都仍然存在。我相信這是因爲克隆方法從textBox中複製了value屬性。當我添加另一個文本框時,有什麼辦法可以擺脫它們嗎?在jQuery中使用克隆方法時擺脫textBox的Value屬性

<html> 
    <head> 
    <title>JQuery Example</title> 
    <script type="text/javascript" src="jquery-1.4.js"></script> 
    <script type="text/javascript"> 


     function removeTextBox() 
     { 
      var childCount = $('p').size() //keep track of paragraph childnodes 

      //this is because there should always be 2 p be tags the user shouldn't remove the first one 
      if(childCount != 2) 
      { 
      var $textBox = $('#textBox') 
      $textBox.detach() 

      } 

     } 


     function addTextBox() 
     { 

     var $textBox = $('#textBox') 
     var $clonedTextBox = $textBox.clone() 

     //document.getElementById('textBox').setAttribute('value', "") 

     $textBox.after($clonedTextBox) 



     } 
    </script> 
    </head> 
    <body> 
    <form id = 
      method="POST" 
      action="http://cs.harding.edu/gfoust/cgi-bin/show"> 

    <p id= "textBox"> 
     Email: 
     <input type = "text" name="email" /> 
     <input type ="button" value ="X" onclick = "removeTextBox()"/> 
    </p> 

     <p> 
     <a href="javascript:addTextBox()">Add another email</a> 
     </p> 
     <input type="submit" value="submit"/> 
    </form> 
    </body> 
</html> 

回答

1

以下此外應該工作:

var $clonedTextBox = $textBox.clone(); 
$($clonedTextBox).val(''); 
+0

都能跟得上它不會在第二行代碼工作 – ProxyProtocol 2010-01-24 22:18:48

+0

VaR的名稱應該是$ clonedTextBox,雖然我覺得在JS開始本地變量用$是一種不好的做法,尤其在與jQuery一起使用時會引起混淆...... – tfwright 2010-01-24 22:45:27