我正在开发一个网站,并希望创建一些输入字段,如果按下按钮,其中一个输入将显示(.innerHTML
)。这是HTML,但我不知道jQuery并搜索了互联网,但找不到任何有用的...
<!doctype html>
<html>
<body>
<input type="text" class="input" placeholder="Enter a word">
<input type="text" class="input" placeholder="Enter a word">
<input type="text" class="input" placeholder="Enter a word">
<input type="button" value="Go!">
</body>
</html>
感谢您帮助我!
答案 0 :(得分:1)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<hr/>
<a href="javascript:;void(0);" id="buttonOne">Insert input form</a>
<hr/>
<input type="submit" value="Post the form" />
</form>
<script type="text/javascript">
var sequence = 0;
$(document).ready(function(){
$("#buttonOne").click(function(){
$("form").prepend("<input name=name"+sequence+" placeholder='something' type='text' /> <br/> ");
sequence++;
});
$("form").submit(function(e){
$('input[type=text]').each(function(){
if($(this).val() == "") $(this).remove();
});
if( $('input[type=text]').size() >= 2 ){
random_number = parseInt( Math.random() * $('input[type=text]').size() );
alert( $('input[type=text]:eq('+random_number+')').val() );
}else{
alert("sorry you need at least 2 fields");
}
return false;
});
});
</script>
&#13;