Does jQuery reference to (this) not work?

I have a href link with the text "attivo" or "non attivo" The
user can set the item to "active" or "closed" in the database using the ajax $ .post () request

I have 2 questions for them:

  • I cannot get a link to $ (this) to work. I tried this with a normal link and it works, but if / else is not completed ??

  • How can I prevent a user from clicking more than once on a link and send multiple requests? Is this a serious problem? Do I need some kind of small timer or something like that?
    At first I thought of a javascript confirmation message, but it was rather annoying for this function.

HTML:

<dl id='album-list'>
<dt id="dt-2">some title</dt>
<dd id="dd-2">
    some description<br />
    <div class='links-right'>status: <a class='toggle-active' href='#'>non attivo</a></div>
</dd>
</dl>

<a class="test" href="#">test</a>

JS:

        $('dd a.toggle-active').click(function() {
            var a_ref = $(this);
            var id = a_ref.parent().parent().attr('id').substring(3);
            if (a_ref.text() == "non attivo") {
                var new_active = "active"; // for db in english
                $.post("ajax-aa.php", {album_id:id, album_active:new_active},
                function(data) {
                    // alert("success");
                    a_ref.text("non attivo"); // change href text
                });
            } else {                
                var new_active = "closed"; // for db in english
                $.post("ajax-aa.php", {album_id:id, album_active:new_active},
                function(data) {
                    // alert("success");
                    a_ref.text("attivo"); // change href text
                });
            }
            return false;
        });     

        $('a.test').click(function() {
            var a_ref = $(this);
            $.post("ajax-aa.php", {album_id:2, album_active:"active"},
            function(data) {
                a_ref.text("changed");
            });
            return false;
        })
+3
2

$(this) if/else, . ,

    function(date) { }

this . this this . , a_ref.

,

  $(this).addClass("hasbeenclicked")

, , :

  if ( ! $(this).is(".hasbeenclicked") ) {
  ....
  }
+5

, , - - , .

a_ref.addClass('in-progress');

, AJAX .

a_ref.removeClass('in-progress');

, , ;

if (a_ref.hasClass('in-progress')) {
    return'
};

: :

$('a.test').click(function() {
    var a_ref = $(this);


    if (a_ref.hasClass('in-progress')) {
        return false;
    } else {
        a_ref.addClass('in-progress');
    }

    $.post("ajax-aa.php", {album_id:2, album_active:"active"},
    function(data) {
        a_ref.text("changed").removeClass('in-progress');
    });

    return false;
})
0

Source: https://habr.com/ru/post/1738772/


All Articles