首页 > 解决方案 > 如何检测一个按钮是否在jquery中被折叠?

问题描述

我有以下按钮:

 <button class="mdl-button mdl-js-button mdl-button--icon js-user-btn is-collapsed" data-quoteid="@user.UserId">
       <i class="icon-chevron-right"></i>
</button>

我在那个按钮上有一个点击事件:

  $('.js-user-btn').click(function() {

  $.ajax({
            type: "POST",
            url: "@(Url.Action("GetUserDetails", "User"))",
            data: {
                "user": id,
                "language": "en-GB"
            },
            success: function (data) {    // I want to trigger the an ajax function here, on the first click of the button. 

On the second the click of the button, the ajax should not be triggered. }

我想在这里触发一个 ajax 函数,在第一次单击按钮时。

在第二次单击按钮时,不应触发 ajax。

标签: javascriptjquery

解决方案


jQuery 有一个方法可以做到这一点:

https://api.jquery.com/one/

从文档:

描述:将处理程序附加到元素的事件。每个事件类型的每个元素最多执行一次处理程序。

在你的情况下:

$('.js-user-btn').one('click', function() {...

推荐阅读