在jQuery中使用clone方法时,摆脱textBox的Value属性

时间:2010-01-24 22:08:02

标签: javascript jquery html

我正在处理这个表单的问题。每当我添加或刷新页面时,值仍然存在。我相信这是因为clone方法从textBox复制value属性。当我添加另一个textBox时,有什么方法可以摆脱它们吗?

<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 个答案:

答案 0 :(得分:1)

以下添加应该有效:

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