如何保留下拉列表的值. [英] how to retain the value of a dropdownlist.

查看:113
本文介绍了如何保留下拉列表的值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况:

我有带有年份列表的下拉列表,我需要检查在下拉列表中选择的值以及在javascript代码中选择的上一个值.

汽车值的下拉模型基于所选年份.

例如:
1)
Dropdownyear-2011
汽车列表的下拉模型.
提交按钮.

2)如果我选择
Dropdownyear-2011再次
汽车的dropdownmodel-应该保留值..

我需要javascipt代码:场景
javascript函数
{
if(previousyearvalueselected == presentyearvalue)
{
然后保留值,不要回发以获得汽车模型
}
其他
回发以获得汽车模型
}

请执行需要的

My scenario:

I have dropdownlist with list of years,i need to check the value selected in the dropdownlist with the previous value selected in javascript code.

Dropdownmodelofthecar values are based on the year selected.

For example:
1)
Dropdownyear-2011
Dropdownmodelofthecar-listof cars.
submit button.

2)If i select
Dropdownyear-2011 again
Dropdownmodelofthecar - values should be retained..

i need the javascipt code :scenario
javascript function
{
if(previousyearvalueselected==presentyearvalue)
{
then retain the value,dont postback to get the carmodels
}
else
postback to get the carmodels
}

please do the needful

推荐答案

var e = document.getElementById(<%= dropdown1.ClientID%>")); //选择元素
var strUser = e.options [e.selectedIndex] .value;

var e2 = document.getElementById(<%= dropdown2.ClientID%>"); //选择元素
var strUser2 = e2.options [e2.selectedIndex] .value;

if(strUser == strUser2)
{
然后保留值,不要回发以获得汽车模型
}
其他
回发以获得汽车模型
var e = document.getElementById("<%=dropdown1.ClientID %>"); // select element
var strUser = e.options[e.selectedIndex].value;

var e2 = document.getElementById("<%=dropdown2.ClientID %>"); // select element
var strUser2 = e2.options[e2.selectedIndex].value;

if(strUser==strUser2)
{
then retain the value,dont postback to get the carmodels
}
else
postback to get the carmodels



请使用以下内容:
Hi
Please use following:
<select name="Dropdownyear" id="Dropdownyear" onchange="doPostbackEvent();">
       <option value="2006">2009</option>
       <option value="2007">2007</option>
       <option value="2008">2008</option>
       <option value="2009">2009</option>
       <option value="2010">2010</option>
   </select>


function doPostbackEvent()
{
   //write your code here...
}



我已将onchange事件添加到下拉列表中,并且当您更改任何选定的值时将调用此事件,否则它将不执行任何操作.
简而言之,当您更改选定的项目时,将调用onchange事件.
这样您就不会编写用于检查项目更改的复杂代码:-\

如有任何疑问,请让我知道.

如果有帮助,请提供"投票",如果这是正确的答案,请提供"接受答案".:rose:

谢谢,
Imdadhusen



i have added onchange event to the dropdown and this event will call when you change any selected value else it do nothing.
in short the onchange event is call when you change selected item.
so you don''t wont to write complex code for checking item changed :-\

Please do let me know, if you have any doubt.

Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen


这篇关于如何保留下拉列表的值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆