0

我有这个,所以当页面加载获取值时,如果为 true,则显示一个有效的 msg,但是当我使用更改时,更改没有,所以我使用实时更改功能,当你更改时有效,但现在默认的负载检查没有?有什么想法吗?

$(document).ready(function() {
                var target = $('.product-options select').find(":selected").val();
                if(target == "2" || target == "4"){
                        $(".beans-msg").html("would you like beans?").show();
                } else {
                    $(".beans-msg").hide();
                }
                console.log(target);
                $('.product-options select').live('change',function(){
                    var changedVal = $(this).find(":selected").val();
                    if(changedVal == "2" || changedVal == "4"){
                        $(".beans-msg").html("would you like beans?").show();
                    } else {
                        $(".beans-msg").hide();
                    }
                    console.log(changedVal);
                });
            });
4

2 回答 2

0

如果是 jQuery 1.5.2,则不支持 on(),但请尝试以下操作:

$(document).ready(function () {
    $('.product-options select').live('change', function() {
        var beans = $.trim( this.value );
        if (beans == "2" || beans == "4") {
            $(this).closest('tr')
                   .find(".beans-msg")
                   .html("would you like beans?").show();
        } else {
            $(this).closest('tr')
                   .find(".beans-msg")
                   .hide();
        }
        console.log(beans);
    }).trigger('change');
});

如果更改功能有效,只需在页面加载时触发它?

于 2013-02-19T00:19:29.237 回答
0
$(document).ready(function() {   
var x = $('.product-options').find(':selected').val();        
        if (x == "2" || x == "4")
            $('.beans-msg').html("would you like beans?").show();
        $('.product-options').live('change',function(){                
            var changedVal = $(this).find(':selected').val();                
                if(changedVal == "2" || changedVal == "4"){
                    $('.beans-msg').html("would you like beans?").show();
                } else {
                    $('.beans-msg').hide();
                }
            });
        });

在这里摆弄

于 2013-02-19T01:18:26.817 回答