我有两个带有打开和关闭按钮的输入,当我打开第二个输入并输入值然后将其删除时,该值保留在输入中,它不会消失,我尝试了一些方法,但它不起作用,在这里是我的代码:
$(function() {
$('#openInput').click(function() {
$('.secondInput').toggle()
});
});
$("#closeInput").click(function () {
$('.secondInput').toggle();
$('.secondInput').val('');
});
.secondInput{
display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text" class="form-control" />
<button type="button" id="openInput">Open</button><br>
<br>
<div class="secondInput">
<input type="text" class="form-control"/>
<button type="button" id="closeInput">Close</button>
</div>
最佳答案
您有两个问题:
- $("#closeInput").click(function () { 没有像其他那样包装在 dom 中准备就绪
- 您没有清空输入字段,而是清空了 div。使用:$('.secondInput input').val('');解决问题
$(function() {
$('#openInput').click(function() {
$('.secondInput').toggle()
});
$("#closeInput").click(function () {
$('.secondInput').toggle();
$('.secondInput input').val('');
});
});
.secondInput {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="form-control"/>
<button type="button" id="openInput">Open</button>
<br>
<br>
<div class="secondInput">
<input type="text" class="form-control"/>
<button type="button" id="closeInput">Close</button>
</div>
关于javascript - 关闭div后将输入值设置为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45221336/