Her*_*ill 5 javascript asp.net
我在页面上有这个CheckBoxList:
<asp:checkboxlist runat="server" id="Locations" datasourceid="LocationsDatasource"
datatextfield="CountryName" datavaluefield="CountryCode" />
Run Code Online (Sandbox Code Playgroud)
我想使用Javascript遍历客户端上的复选框元素并获取每个选中复选框的值,但这些值似乎在客户端不可用.HTML输出如下所示:
<table id="ctl00_Content_Locations" class="SearchFilterCheckboxlist" cellspacing="0" cellpadding="0" border="0" style="width:235px;border-collapse:collapse;">
<tr>
<td><input id="ctl00_Content_Locations_0" type="checkbox" name="ctl00$Content$Locations$0" /><label for="ctl00_Content_Locations_0">Democratic Republic of the Congo</label></td>
</tr><tr>
<td><input id="ctl00_Content_Locations_1" type="checkbox" name="ctl00$Content$Locations$1" /><label for="ctl00_Content_Locations_1">Central African Republic</label></td>
</tr><tr>
<td><input id="ctl00_Content_Locations_2" type="checkbox" name="ctl00$Content$Locations$2" /><label for="ctl00_Content_Locations_2">Congo</label></td>
</tr><tr>
<td><input id="ctl00_Content_Locations_3" type="checkbox" name="ctl00$Content$Locations$3" /><label for="ctl00_Content_Locations_3">Cameroon</label></td>
</tr><tr>
<td><input id="ctl00_Content_Locations_4" type="checkbox" name="ctl00$Content$Locations$4" /><label for="ctl00_Content_Locations_4">Gabon</label></td>
</tr><tr>
<td><input id="ctl00_Content_Locations_5" type="checkbox" name="ctl00$Content$Locations$5" /><label for="ctl00_Content_Locations_5">Equatorial Guinea</label></td>
</tr>
Run Code Online (Sandbox Code Playgroud)
无法找到值("cd","cg","ga"等).他们在哪?甚至可以在客户端访问它们,还是我需要使用转发器或其他东西自己构建这个复选框?
Ada*_*amE 15
我终于得到了我一直在寻找的答案!
实际上,asp.net CheckboxList控件确实将value属性呈现为HTML - 它已经在我的生产站点中工作了一年多了!(我们总是关闭所有网站的EnableViewState,它仍然有效,没有任何调整或黑客攻击)
然而,突然间,它停止工作一天,我们的CheckboxList复选框不再在HTML中呈现它们的value属性!WTF你说?我们也是!这需要一段时间来解决这个问题,但由于它以前一直在工作,我们知道必须有一个理由.原因是我们的web.config意外更改了!
<pages controlRenderingCompatibilityVersion="3.5">
Run Code Online (Sandbox Code Playgroud)
我们从页面配置部分删除了这个属性,这就是诀窍!
为什么会这样?我们反映了CheckboxList控件的代码,并在RenderItem方法中找到了它:
if (this.RenderingCompatibility >= VersionUtil.Framework40)
{
this._controlToRepeat.InputAttributes.Add("value", item.Value);
}
Run Code Online (Sandbox Code Playgroud)
最亲爱的开发兄弟姐妹不要绝望!我在Stackexchange上搜索到的所有答案以及网络的其他部分都给出了错误的信息!从.Net 4.0开始,asp.net呈现CheckboxList复选框的value属性:
<input type="checkbox" value="myvalue1" id="someid" />
Run Code Online (Sandbox Code Playgroud)
也许没有实际用处,微软让你能够在你的web.config中添加一个"controlRenderingCompatibilityVersion",通过设置低于4的版本来关闭它,这对我们来说是完全没必要的,实际上是有害的,因为javascript代码依赖在值属性上...
对于我们所有的复选框,我们得到chk.val()等于"on",这是最初提醒我们这个问题的原因(使用jquery.val()获取复选框的值.结果"打开" "是选中的复选框的值...每天学习一些东西.
小智 6
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="tx" DataValueField="vl">
</asp:CheckBoxList>
</div>
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
</form>
</body>
<script type="text/javascript">
function Button1_onclick()
{
var itemarr = document.getElementById("CheckBoxList1").getElementsByTagName("span");
var itemlen = itemarr.length;
for(i = 0; i <itemlen;i++)
{
alert(itemarr[i].getAttribute("dvalue"));
}
return false;
}
</script>
Run Code Online (Sandbox Code Playgroud)
码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("tx");
dt.Columns.Add("vl");
DataRow dr = dt.NewRow();
dr[0] = "asdas";
dr[1] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "456456";
dr[1] = "2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "yjryut";
dr[1] = "3";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "yjrfdgdfgyut";
dr[1] = "3";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "34534";
dr[1] = "3";
dt.Rows.Add(dr);
CheckBoxList1.DataSource = dt;
CheckBoxList1.DataBind();
foreach (ListItem li in CheckBoxList1.Items)
{
li.Attributes.Add("dvalue", li.Value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13679 次 |
| 最近记录: |