Home  >  Article  >  Web Front-end  >  Detailed explanation of how to use Button’s click event in jQuery

Detailed explanation of how to use Button’s click event in jQuery

黄舟
黄舟Original
2017-06-27 09:58:225349browse

I just came into contact with jQuery today

  $("#Button1").click( function() { alert('我被点了') });

There is no response after clicking Button1, please help to modify it

The final desired effect is that after clicking Button1, Determine whether the value of textbox1 is empty. If it is empty, pop up "alert('textbox1 cannot be empty')"

 $(document).ready(function(){
             $("#Button1").bind("click",
                 function() {
                       if($.trim($("#textbox1").val())=="")  
                        {alert('我被点了');}
                   });

});

It should be noted that # is just a modifier, not the person part of the ID

<button id="Button1">Button1</button>
<button id="Button1">Button1</button>



$("#Button1").click(function(){
    if( $('.textbox1').val() == "" ){
         alert('textbox1不能为空');
    }
})

Maybe you didn’t add

$(document).ready(function(){
});

...

Maybe it’s the control# of your Button1 ID ##has not been loaded yet. Just load it out or you can put the JS code for execution later instead of in the
Head part, which is also good for SEO.

$(function(){
   $(&#39;#Button1&#39;).click(function(){
            $(&#39;#textbox1&#39;).val()==&#39;&#39;?alert(&#39;textbox1不能为空&#39;):alert(&#39;我被点击了&#39;);    
        });
})

The above is the detailed content of Detailed explanation of how to use Button’s click event in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn