Home > Web Front-end > JS Tutorial > body text

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

黄舟
Release: 2017-06-27 09:58:22
Original
5397 people have browsed it

I just came into contact with jQuery today

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

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('我被点了');}
                   });

});
Copy after login

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

<button id="Button1">Button1</button>
Copy after login
<button id="Button1">Button1</button>



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

Maybe you didn’t add

$(document).ready(function(){
});
Copy after login

...

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;);    
        });
})
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template