如何在选择控件中以编程方式选择适当的选项 [英] How can I select the appropriate option programmatically in select control

查看:93
本文介绍了如何在选择控件中以编程方式选择适当的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var ch;




<select name="cmbStatus">
    <option value= 'I'>impossible</option>
    <option value= 'U'>unvisited</option>
    <option value= 'P'>priced</option>
    <option value= 'W'>in progress</option>
    <option value= 'R'>ready</option>                          
</select>





如何根据ch的值以编程方式选择适当的选项? (例如,如果值为''W''在select控件中选择''正在进行''



How can I select the appropriate option programmatically based on the value of ch? (For example if value is ''W'' select ''in progress'' in the select control )

推荐答案

我不确定是否理解了你的问题,但如果您的目标是根据变量的值选择一个选项,这将实现您的期望:

I''m not sure about having understood your question, but if your goal is to select an option according with the value of a variable, this accomplishes your expectations:
<select name="cmbStatus">
    <option value= 'I' @(ch == "I" ? "selected = 'selected'" : "")>impossible</option>
    <option value= 'U' @(ch == "U" ? "selected = 'selected'" : "")>unvisited</option>
    <option value= 'P' @(ch == "P" ? "selected = 'selected'" : "")>priced</option>
    <option value= 'W' @(ch == "W" ? "selected = 'selected'" : "")>in progress</option>
    <option value= 'R' @(ch == "R" ? "selected = 'selected'" : "")>ready</option>
</select>


如何根据ch 的值以编程方式选择合适的选项

使用JavaScript可以设置如下:

How can I select the appropriate option programmatically based on the value of ch
Using JavaScript you can set like:
function setDropDownSeletedValue(selectedValue)
{
    var myCB=document.getElementById('myCB');
    myCB.value=selectedValue;
}





现在,直接从代码后面设置值或直接设置JS:



Now, set value from code behind or JS directly:

//Get value from DB
string valueFromDB = "W";//e.g value in DB is 'Second'
string script = string.Format("setDropDownSeletedValue('{0}')", valueFromDB);
ClientScript.RegisterStartupScript(this.GetType(), "KeyValue", script, true);


这篇关于如何在选择控件中以编程方式选择适当的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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