以下代码提供了无限数量的输入框。我想要的是限制要添加的文本框的数量。它应该只有3个。
<?php
if (isset($_POST['submitted'])){
print_r($_POST);
}
?>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#button_add_input").click(function(event){
var textArea = "<textarea name='new_quote[]' />";
$("#div_quotes").append(textArea);
$("#div_quotes").append("\n<br />");
});
});
</script>
</head>
<body>
<form method="post">
<div id="div_quotes"></div>
<input type="button" value="Add Text Area" id="button_add_input">
</form>
</body>
</html>
答案 0 :(得分:0)
您可以使用JQuery descendant selector来计算div中textareas的数字(使用.length
)。
<?php if (isset($_POST[ 'submitted'])){ print_r($_POST); } ?>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#button_add_input").click(function(event) {
if ($("#div_quotes textarea").length < 3) {
var textArea = "<textarea name='new_quote[]' />";
$("#div_quotes").append(textArea);
$("#div_quotes").append("\n<br />");
}
});
});
</script>
</head>
<body>
<form method="post">
<div id="div_quotes"></div>
<input type="button" value="Add Text Area" id="button_add_input">
</form>
</body>
</html>