开发者

Why this Jquery code does not run?

开发者 https://www.devze.com 2023-02-05 03:45 出处:网络
I have Jquery code that checks on .change status of HTML select element and when there\'s change in select element it should check if certain radio button is active.

I have Jquery code that checks on .change status of HTML select element and when there's change in select element it should check if certain radio button is active.

 $('#select_ele').change(function(){
     //
     //... Doing all kind of fancy stuff here
     //
     if($('input[name="radio_button"]').val() == 'bp'){
         alert('YO'); 
     }
 });

So the idea here was on change of select check if 'bp' radio button is active and if so then al开发者_如何学运维ert me.


Your code will always give you the .val() of the first name="radio_button" match it finds, whether or not it is checked.

Instead, you should use the checked-selector(docs) to get the one that is checked.

$('input[name="radio_button"]:checked').val() == 'bp'


Your code is probably running before the document loads.
Therefore, the selector isn't matching anything (since the document hasn't been parsed yet), and the script doesn't do anything Move the <script> block to the bottom of the page or wrap it in $(function() { ... }) (this executes it in the document load event).


Think this is what you're looking for:

    if($('input[name="bp"]').is(":checked"){ alert("yo"); })


Please this in the middle, it will help you work it out.

alert("Hello I am Working")

As well as this.

console.log(event.type);
0

精彩评论

暂无评论...
验证码 换一张
取 消