如何只做一次jQuery事件? [英] How to make a jQuery event only once?

查看:75
本文介绍了如何只做一次jQuery事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击删除"时,将调用功能deleting().即使多次单击删除",如何也只能添加一次?我希望它只是在我第一次点击时发生.

When I click on "DELETE", the function deleting() is called. How can I prepend only once, even if I click on DELETE many times? I want it to happen just the first time I click.

function deleting() {
  $(".checking").prepend("<input type='checkbox' name='idcheckbox'>");
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="javascript:deleting()">DELETE</a>
<div class="checking"></div>

推荐答案

您可以使用 jQuery one 为了:将处理程序附加到元素的事件.每种事件类型的每个元素最多只能执行一次处理程序.

You may use jQuery one in order to: Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

您可以动态地而不是内联地附加事件:

You may attach the event dynamically and not inline:

    $(function () {
        $('a').one('click', function(e) {
            e.preventDefault();
            $(".checking").prepend("<input type='checkbox' name='idcheckbox'>");
        })
    });

您的html可能类似于:

And you html could be like:

<form>
 <a href="">DELETE</a>
</form>

这篇关于如何只做一次jQuery事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆