admin管理员组文章数量:1357615
I wonder why it isn't working.
Here is my JS:
$("#cat-code").change(function(){
var value = $("#cat-code option:selected").val();
if (value == "10400000"){
$("#div1").show();
$("#div2").hide();
}
});
Select Tag:
<select class="cat-code" id="cat-code" name="project_procurement_management_plan[items_attributes][0][category_id]">
<option value="1">10400000</option>
<option value="2">10401000</option>
</select>
Divs to show or hide:
<div class="div1">hi</div>
<div class="div2">hello</div>
Any workarounds will be appreciated. Thanks.
I wonder why it isn't working.
Here is my JS:
$("#cat-code").change(function(){
var value = $("#cat-code option:selected").val();
if (value == "10400000"){
$("#div1").show();
$("#div2").hide();
}
});
Select Tag:
<select class="cat-code" id="cat-code" name="project_procurement_management_plan[items_attributes][0][category_id]">
<option value="1">10400000</option>
<option value="2">10401000</option>
</select>
Divs to show or hide:
<div class="div1">hi</div>
<div class="div2">hello</div>
Any workarounds will be appreciated. Thanks.
Share Improve this question edited Mar 23, 2013 at 23:57 dda 6,2132 gold badges27 silver badges35 bronze badges asked Mar 23, 2013 at 23:56 xirukitepexirukitepe 1,6157 gold badges27 silver badges54 bronze badges 1- replace class with id in div tags. – alioguzhan Commented Mar 24, 2013 at 0:01
2 Answers
Reset to default 5It looks like your JS is reference to DOM objects with IDs, but your HTML has classes.
Try this JS instead:
$(".div1").show();
$(".div2").hide();
You've specifically set the values to 1 and 2, so that's probably what they are then:
$("#cat-code").change(function(){
var value = this.value;
if (value == "1"){
$(".div1").show();
$(".div2").hide();
}
});
FIDDLE
The options text will only be used as the selects value if the option does not have a value, and to get the selected value all you need is this.value
inside the handler for the select.
本文标签: jqueryShowhide div based on selected option value in JavascriptStack Overflow
版权声明:本文标题:jquery - Showhide div based on selected option value in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744076357a2586820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论