.net js获取radio或dropdownlist值并赋值到文本框

 第一种:radio

页面:

<div class="td_margin radio" style="margin-left: 1.5px">
常用处理意见:<input type="radio" name="oper" value="阅" onclick="getRadio(this)">阅
&nbsp;&nbsp; <input type="radio" name="oper" value="阅(已终止下发)" onclick="getRadio(this)">阅(已终止下发)
</div>

<div class="td_margin">
<asp:TextBox ID="tbComment" runat="server" ></asp:TextBox>
</div>

 

js脚本:

<script type="text/javascript">
//求单选按纽的值,适用单选项及多选项。未选返回false;有选择项,返回选项值。选项值并赋值给文本框。
function getRadio(oRadio) {
var oRadioLength = oRadio.length;
var oRadioValue = false;
//alert("oRadioLength:["+oRadioLength+"]");

if (oRadioLength == undefined) {
if (oRadio.checked) {
oRadioValue = oRadio.value;
}
}
else {
for (i = 0; i < oRadioLength; i++) {
//alert("oRadio["+i+"]:"+oRadio[i].checked+"/"+oRadio[i].value);
if (oRadio[i].checked) {
oRadioValue = oRadio[i].value;
break;
}
}
}
document.getElementById("<%=tbComment.ClientID%>").value = oRadioValue;
}
</script>

效果图:

 

第二种:DropDownList

 

页面:

<div class="td_margin">
<asp:TextBox ID="tbComment" runat="server" ></asp:TextBox>
<asp:DropDownList ID="ddlFreqComment" runat="server">
</asp:DropDownList>
</div>

 

后台:

WorkflowWebHelper.SetReadOrStopComment(rbFreqComment, tbComment);

 

公共类方法:

/// <summary>
/// 设置处理意见选择框(阅或者阅(已终止下发))
/// </summary>
/// <param name="ddlFreqComment"></param>
/// <param name="tbComment"></param>
public static void SetReadOrStopComment(DropDownList ddlFreqComment, TextBox tbComment)
{
IList<string> generalList = new List<string>();
generalList.Add("阅");
generalList.Add("阅(已终止下发)");
ddlFreqComment.DataBind(generalList.Select(s => new ListItem(s, s))
, ControlDefine.CreateListItem("请选择.."));
ddlFreqComment.Attributes.Add("onchange"
, "var v = this.options[this.options.selectedIndex].value; if(v != '') document.getElementById('" + tbComment.ClientID + "').value = v;");
}

效果图:

 

posted @ 2019-08-22 09:41  suqq小白  阅读(350)  评论(0编辑  收藏  举报