[英]Jquery for retaining the selected value in select option
i am playing some show/hide function using my select drop down. 我正在使用我的选择下拉菜单播放一些显示/隐藏功能。 Here is the html for select, 这是供选择的html,
<select id="sel1" size="1" name="">
<option selected="selected" value="0">Select Here</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
Here is my jquery, 这是我的jQuery,
$(document).ready(function(){
$("#sel1").change(fun);
});
function fun(){
var selected = $("#sel1 option:selected");
if(selected.val() == 1) {
$("#div1").slideDown("slow");
$('#div2').slideUp("slow");
} else if(selected.val() == 2) {
$("#div1").slideUp("slow");
$('#div2').slideDown("slow");
} else {
$("#div1").slideUp("slow");
$('#div2').slideUp("slow");
}
};
But after the value sent to the server and return back, I want to retain the div1 or div2 based on the value select in the dropdown. 但是,在将值发送到服务器并返回之前,我想根据下拉列表中选择的值保留div1或div2。 But as of now, both the div are not displaying because of display=none at initial level. 但是到目前为止,由于在初始级别display = none,两个div都没有显示。 Any way to retain this? 有什么办法保留这个吗?
If your HTML is generated using a server-side language, your best option would be to only add the display: none
CSS property to them when appropriate. 如果您的HTML是使用服务器端语言生成的,则最好的选择是仅在其上添加display: none
CSS属性。 Alternatively, you could just trigger the change
event handler when the DOM is ready, using: 另外,您可以在DOM准备就绪时触发change
事件处理程序,方法是:
$(document).ready(function(){
$("#sel1").change(fun).trigger('change');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.