我是这个领域的初学者。我创建了一个 HTML 页面,单击一些“+”/“-”按钮,我分别从 Multiple.js 文件中调用 add() 和 remove() 以从 HTML 页面中添加或删除代码块。但我得到了意想不到的结果。文件如下:Multiple Group Selection.html
<!DOCTYPE HTML >
<HTML>
<HEAD>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<link rel="stylesheet" href="style.css">
<TITLE>Multiple Group Selections</TITLE>
<script src="Multiple.js" type="text/javascript"></script>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<H2 ALIGN="CENTER">Multiple Group Selections</H2>
<div id="clonedInput1" class="clonedInput">
<FIELDSET class="field">
<LEGEND ALIGN="RIGHT">
<button type="button" class = "clone" onclick={add();} >+</button>
<button type="button" class = "remove" onclick={remove();}>-</button>
</LEGEND>
Field 2A: <INPUT TYPE="TEXT" NAME="field2A" VALUE="Field A"><BR>
Field 2B: <INPUT TYPE="TEXT" NAME="field2B" VALUE="Field B"><BR>
Field 2C: <INPUT TYPE="TEXT" NAME="field2C" VALUE="Field C"><BR>
</FIELDSET>
</div>
</BODY>
</HTML>
多个.js:
function add(){
var regex = /^(.*)(\d)+$/i;
var cloneIndex = $(".clonedInput").length;
$("button.clone").live("click", function(){
$(this).parents(".clonedInput").clone()
.appendTo("body")
.attr("id", "clonedInput" + cloneIndex)
.find("*").each(function() {
var id = this.id || "";
var match = id.match(regex) || [];
if (match.length == 3) {
this.id = match[1] + (cloneIndex);
}
});
cloneIndex++;
});
}
function remove(){
$("button.remove").live("click", function(){
$(this).parents(".clonedInput").remove();
});
}
样式.css
.clone {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
color: #00ffff;
Background-COLOR: white;
border: black;
border-style: solid;
border-width: 1px;
}
.remove{
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
color: #00ffff;
Background-COLOR: white;
border: black;
border-style: solid;
border-width: 1px;
}
我想要的只是单击“+”按钮,必须将其附加到 . 单击“-”按钮时,它也应该被删除。请帮帮我。我累死。