开发者

Where is my mistake using JavaScript and jQuery

开发者 https://www.devze.com 2023-04-09 22:09 出处:网络
I have a question a开发者_StackOverflow社区ctually I need one if/else for hide or show one div, I had wrote the following function but it didn’t work:

I have a question a开发者_StackOverflow社区ctually I need one if/else for hide or show one div, I had wrote the following function but it didn’t work:

jQuery(document).ready(function(){   /*show div OtraUniversidad when option:selected = 165*/
  var optionValue = $("#Universidad option:selected").val();
  $("#OtraUniversidad").hide();  
  if(optionValue == 165){
    $("#OtraUniversidad").show();
  }
});

Actually works: $("#OtraUniversidad").hide();

I don't know what's wrong; I'm new in JavaScript and jQuery

Some help is always welcome.


I think this should work:

jQuery(document).ready(function() {
    $('#Universidad').change(function() {
        var optionValue = $("#Universidad").val();

        if(optionValue == 165) {
            $("#OtraUniversidad").show();
        } else {
            $("#OtraUniversidad").hide();
        }
    }).change();
});

Demo: http://jsfiddle.net/gGX2E/1/


optionValue == 165 

This comparison is wrong, you must use this:

if(optionValue == "165")
0

精彩评论

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