开发者

Jquery If statement

开发者 https://www.devze.com 2023-03-22 05:42 出处:网络
I\'m trying 开发者_C百科to hide and show a option based on it\'s class. Can anyone help me see why this isn\'t working?

I'm trying 开发者_C百科to hide and show a option based on it's class. Can anyone help me see why this isn't working?

           $(document).ready(function(){
                $("#product_boutique").val( <? echo $rowO->product_boutique ?> );
                $("#product_category").val( '<? echo $rowO->category ?>' );
                $("#product_sub_category").val( '<? echo $rowO->category ?>' );
                var select = $("#product_category").val( '<? echo $rowO->category; ?>' ).val();

                if (select == 'womenswear'){
                      $('.mens').hide();
                      $('.womens').show();
                }
                if (select == 'menswear'){
                      $('.mens').show();
                      $('.womens').hide();
                }   

            });


var select = $("#product_sub_category").val( '<? echo $rowO->category; ?>' );
alert(typeof select); // object

Because .val('...') returns a jquery object.

Try

var select = $("#product_sub_category").val( '<? echo $rowO->category; ?>' ).val();

By doing that, you set the value to the #product_sub_category and you retrieve it to variable select.


The jQuery val(value) method returns a jQuery object, not a string. If you change:

var select = $("#product_sub_category").val( '<? echo $rowO->category; ?>' );

to:

var select = $("#product_sub_category").val( '<? echo $rowO->category; ?>' ).val();

it should work fine. Because the val(value) method returns a jQuery object, you can chain it with other jQuery methods as I've done above, so 2 calls to val in the same line will work fine.


When you are passing argument to .val(), as in your case, you are setting the value. To get the current value, you have to call .val() without arguments.

$("#product_sub_category").val( '<? echo $rowO->category; ?>' );
var select = $("#product_sub_category").val();


You have:

if (select == 'womenswear'){
    $('.mens').hide();
    $('.womens').show();
}
if (select == "menswear"){
    $('.mens').hide();
    $('.womens').show();
}   

It should be:

if (select == 'womenswear'){
    $('.mens').hide();
    $('.womens').show();
}
if (select == "menswear"){
    $('.mens').show();
    $('.womens').hide();
}   
0

精彩评论

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