How to execute Jquery code only once??

后端 未结 9 2066
梦谈多话
梦谈多话 2020-12-16 15:52

I have one jquery method and i used call that method on click of a button . So in that code i have one line \" $(\"#loadingMessage\").css(\'padding-top\',\'6%\');

相关标签:
9条回答
  • 2020-12-16 16:36

    use .one for once and use click for others.check below

      <script>
        $(document).ready(function() {
        $('#SearchButton').one('click', function() {
             $("#loadingMessage").css('padding-top','6%'); //I want this line execute only once, i mean first time
    
        });    
    
    
    $('#SearchButton').click(function() {                 
             $(".spinner").css('visibility','hidden');
    
    
             loaderStart();
             document.getElementById('loadinggif3').style.display = "block";
             $("#loadinggif3").show();
             $(".col-sm-9").css('visibility','hidden');
              var str = $('#SearchText').val();
              str = str.trim();
             if(str=="") return false;
            });
    
    
      } ); 
    
           </script>  
    
    0 讨论(0)
  • 2020-12-16 16:37

    quick and 'dirty' solution:

    $.first_time = true;
    $('#SearchButton').click(function() {                 
        if($.first_time == true) $("#loadingMessage").css('padding-top','6%');
        $.first_time = false;
    });
    

    some explanation: you need a global jQuery variable here ($.first_time in this example), so that it's still known inside the anonymous function of the click event.

    0 讨论(0)
  • 2020-12-16 16:43

    you can use bolean in all languages.

    in jquery:

    var isFirst = true;
    
    if(isFirst){  
    $("#loadingMessage").css('padding-top','6%');  
    isFirst=false;  
    }
    
    0 讨论(0)
提交回复
热议问题