我有一个 html 选择和一个添加按钮。如果有人单击添加按钮,则 html 选择创建相同的类,名称。我的问题是如果我更改第一个 html 选择然后更改功能正常工作但如果我更改创建的 html 选择然后更改功能不起作用。请有人能指出我在这里可能做错了什么吗?非常感谢。这是我的代码:
<html>
<head>
<title>the title</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$(document).on('change','.name',function(){
calculateSum();
});
$(document).on('click','.del',function(){
calculateSum();
});
});
function calculateSum() {
$.post(
"data.php",
$(".name").serialize(),
function(data) {
$('#stage1').html(data);
}
);
}
</script>
<script type="text/javascript" language='javascript'>
/*
This script is identical to the above JavaScript function.
*/
var ct = 1;
function new_link()
{
ct++;
var div1 = document.createElement('div');
div1.id = ct;
// link to delete extended form elements
var delLink = '<div class="del" style="text-align:right;margin-top:-20px"><a href="javascript:delIt('+ ct +')">Delete</a></div>';
div1.innerHTML = document.getElementById('newlinktpl').innerHTML + delLink;
document.getElementById('newlink').appendChild(div1);
}
// function to delete the newly added set of elements
function delIt(eleId)
{
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('newlink');
parentEle.removeChild(ele);
}
</script>
</head>
<body>
<div id="stage1" style="background-color:blue; color: white">
STAGE - 1
</div>
<form id="testform">
<div id="newlink">
<table>
<tr>
<td><p>Fruit:</p></td>
<td>
<select class="name" name="name[]">
<option>Apple</option>
<option>Mango</option>
<option>Orange</option>
<option>Banana</option>
</select>
</td>
</tr>
</table>
</div>
<input type="button" value="Add" onclick="javascript:new_link()"/>
</form>
<div id="newlinktpl" style="display:none">
<table>
<tr>
<td><p>Fruit:</p></td>
<td>
<select class="name" name="name[]">
<option>Apple</option>
<option>Mango</option>
<option>Orange</option>
<option>Banana</option>
</select>
</td>
</tr>
</table>
</div>
</body>
</html>
php代码:
<?php
$fruit=$_REQUEST["name"];
$n = count($fruit);
for($i=0;$i<$n; $i++)
{
echo $fruit[$i]."<br/>";
}
?>