gpt4 book ai didi

javascript - 在 jQuery 中使用克隆方法时摆脱文本框的 Value 属性

转载 作者:行者123 更新时间:2023-11-28 02:57:52 24 4
gpt4 key购买 nike

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

<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>

最佳答案

以下添加应该有效:

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

关于javascript - 在 jQuery 中使用克隆方法时摆脱文本框的 Value 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2129098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com