有一个代码
<!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<title></title>
</head>
<body>
<input type="submit" name="" id="add">
<div id="elements"> </div>
<script type="text/javascript">
var counter = 0;
$(document).on('click', '#add', function (e) {
counter++;
$("#elements").html($("#elements").html()+
'<input type="text" name="" id="input-'+counter+'">');
$("#input-"+counter).val("10");
});
</script>
</body>
</html>
当您单击该按钮时,将添加一个新字段。它应该有一个默认值 10。为什么要重置以前的字段?如何解决?
改用:
.append()_.html()