如何在按钮单击 JS 后停止触发事件
How to stop triggering an event after button click JS
我编写了一个函数,用于在用户单击按钮时显示 2 个文本框。但问题是当单击这两个按钮时,会触发表单中另一个按钮的事件。所以我为隐藏和显示功能编写了一个鼠标单击事件。但它仍然不起作用。单击显示按钮时,会显示 2 个文本框,单击隐藏按钮时,它们会消失。我会把我的代码写在下面。
<div class="form-group">
<div class="col-sm-9">
<input type="text" id="height"/>
<input type="text" id="width"/>
<button id="hide">Hide</button>
<button id="show">Show</button>
</div>
</div>
$("show").click(function(){ #Mouse Click event for show button
$(document).ready(function(){
$("#input1").hide();
$("#input2").hide();
$("#hide").click(function(){
$("#input1").hide();
$("#input2").hide();
});
$("#show").click(function(){
$("#input1").show();
$("#input2").show();
});
});
});
试试这个
$(document).ready(function() {
$("#height, #width").hide();
$("#hide").click(function() {
$("#height, #width").hide();
});
$("#show").click(function() {
$("#height, #width").show();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<div class="col-sm-9">
<input type="text" id="height" />
<input type="text" id="width" />
<button type="button" id="hide">Hide</button>
<button type="button" id="show">Show</button>
</div>
</div>
直接在document.ready里面调用点击函数即可。还使用适当的选择器在适当的元素上触发事件
$(document).ready(function() {
$("#hide").click(function() {
$("#height ,#width").hide();
});
$("#show").click(function() {
$("#height ,#width").show();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<div class="col-sm-9">
<input type="text" id="height" />
<input type="text" id="width" />
<button id="hide">Hide</button>
<button id="show">Show</button>
</div>
</div>
我编写了一个函数,用于在用户单击按钮时显示 2 个文本框。但问题是当单击这两个按钮时,会触发表单中另一个按钮的事件。所以我为隐藏和显示功能编写了一个鼠标单击事件。但它仍然不起作用。单击显示按钮时,会显示 2 个文本框,单击隐藏按钮时,它们会消失。我会把我的代码写在下面。
<div class="form-group">
<div class="col-sm-9">
<input type="text" id="height"/>
<input type="text" id="width"/>
<button id="hide">Hide</button>
<button id="show">Show</button>
</div>
</div>
$("show").click(function(){ #Mouse Click event for show button
$(document).ready(function(){
$("#input1").hide();
$("#input2").hide();
$("#hide").click(function(){
$("#input1").hide();
$("#input2").hide();
});
$("#show").click(function(){
$("#input1").show();
$("#input2").show();
});
});
});
试试这个
$(document).ready(function() {
$("#height, #width").hide();
$("#hide").click(function() {
$("#height, #width").hide();
});
$("#show").click(function() {
$("#height, #width").show();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<div class="col-sm-9">
<input type="text" id="height" />
<input type="text" id="width" />
<button type="button" id="hide">Hide</button>
<button type="button" id="show">Show</button>
</div>
</div>
直接在document.ready里面调用点击函数即可。还使用适当的选择器在适当的元素上触发事件
$(document).ready(function() {
$("#hide").click(function() {
$("#height ,#width").hide();
});
$("#show").click(function() {
$("#height ,#width").show();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<div class="col-sm-9">
<input type="text" id="height" />
<input type="text" id="width" />
<button id="hide">Hide</button>
<button id="show">Show</button>
</div>
</div>