如何在checkboxlist控件的列表中获取值并使用它进行验证
how to get a value in a list on a checkboxlist control and use it to validate
我在asp.net 3.5中有一个表单,它有一个母版页和一个子版页(内容页)。该表单有几个问题,我使用asp.net复选框列表作为问题,因为可以选择多个选项,如果用户在其中一个选项上选择"其他",则他们必须在文本框字段中输入数据。
我制作了以下客户端javascript来验证这一点,但代码似乎没有检查是否选择了Other选项值,我认为这与html在页面上的呈现方式有关,,
你能建议一下怎么做吗?
提前谢谢。
渲染Javascript
//Here I am trying to get the text property of the label rendered for the texbox
// and set my validation arguments
<script language='javascript' type='text/javascript'>
function checkOther2(oSrc, args) {
{
var elementRef =
document.getElementById('ctl00_Content_CheckBoxList1');
var checkBoxArray = elementRef.getElementsByTagName('input');
var checkedValues = '';
for (var i = 0; i < checkBoxArray.length; i++) {
var checkBoxRef = checkBoxArray[i];
if (checkBoxRef.checked == true) {
// You can only get the Text property, which
will be in an HTML label element.
var labelArray =
checkBoxRef.parentNode.getElementsByTagName('label');
if (labelArray.length > 0) {
if (checkedValues.length > 0)
checkedValues += ',';
checkedValues += labelArray[0].innerHTML.text;
if (checkedValues == 'Other') {
args.IsValid = !(args.Value == "")
// test
alert("Hello");
}
}
else {
args.IsValid = true;
}
}
}
}
}
// HTML Rendered
<tr>
<td style="height: 20px">
</td>
</tr>
<tr>
<td style="font-weight: 700">
2.- What did you like most about working here?<strong>
Check all that apply
<span id="ctl00_Content_CheckBoxListValidator1"
style="color:Red;display:none;"></span>
</strong><br /> </td>
</tr>
<tr>
<td>
<table id="ctl00_Content_CheckBoxList1"
class="myradioButton" border="0">
<tr>
<td><input id="ctl00_Content_CheckBoxList1_0" type="checkbox"
name="ctl00$Content$CheckBoxList1$0" /><label
for="ctl00_Content_CheckBoxList1_0">Staff</label></td>
</tr><tr>
<td><input id="ctl00_Content_CheckBoxList1_1" type="checkbox"
name="ctl00$Content$CheckBoxList1$1" /><label
for="ctl00_Content_CheckBoxList1_1">Facility</label></td>
</tr><tr>
<td><input id="ctl00_Content_CheckBoxList1_2" type="checkbox"
name="ctl00$Content$CheckBoxList1$2" /><label
for="ctl00_Content_CheckBoxList1_2">Pay</label></td>
</tr><tr>
<td><input id="ctl00_Content_CheckBoxList1_3" type="checkbox"
name="ctl00$Content$CheckBoxList1$3" /><label
for="ctl00_Content_CheckBoxList1_3">Other</label></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
If other, please elaborate:<br />
<input name="ctl00$Content$txt2other"
type="text" id="ctl00_Content_txt2other" class="txtOther" />
<span id="ctl00_Content_CustomValidator3"
style="color:Red;font-weight:700;visibility:hidden;">Please enter a
comment in question #2.</span>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td style="font-weight: 700">
2.- What did you like most about working here?<strong>
Check all that apply
<cc1:CheckBoxListValidator
ID="CheckBoxListValidator1" runat="server"
ControlToValidate="CheckBoxList1" Display="None"
ErrorMessage="Question 2 is
Required"></cc1:CheckBoxListValidator>
</strong><br /> </td>
</tr>
<tr>
<td>
----------- Actual Markup on asp.net form
<asp:CheckBoxList ID="CheckBoxList1"
runat="server" CssClass="myradioButton">
<asp:ListItem Text="Staff"
Value="Staff">Staff</asp:ListItem>
<asp:ListItem Text="Facility"
Value="Facility">Facility</asp:ListItem>
<asp:ListItem Text="Pay"
Value="Pay">Pay</asp:ListItem>
<asp:ListItem Text="Other"
Value="Other">Other</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td>
If other, please elaborate:<br />
<asp:TextBox ID="txt2other" runat="server"
CssClass="txtOther"></asp:TextBox>
<asp:CustomValidator
ID="CustomValidator3" runat="server"
ClientValidationFunction="checkOther2"
ControlToValidate="txt2other"
ErrorMessage="Please enter a comment in
question #2." style="font-weight: 700"
ValidateEmptyText="True"></asp:CustomValidator>
</td>
</tr>
您的JS看起来相当复杂。尝试更简单的方法。。。
function isValid(f)
{
var cb = document.getElementById('<%=CheckBoxList1_3.ClientID%>');
if(cb && cb.checked)
{
var tb = document.ElementById('<%=txt2other.ClientID%>');
if(tb && tb.value.length > 0)
{
f.submit();
}
else
{
alert('Please enter a comment in question #2.');
}
}
}
如果你有很多这样的东西,试着在复选框上设置一个属性,比如value=other
,这样当你循环检查复选框时,你就可以使用if(cb.checked && cb.value == 'other')
相关文章:
- 身份验证后获取Facebook图片
- 获取json结果的身份验证问题
- 使用xPath从Jquery数据验证程序获取输入
- Sequelize.js中获取验证器错误
- 电话号码没有't获取经过验证的javascript
- 正在尝试获取有关已验证用户的信息
- 从 Google 获取当前位置,而无需将用户送去进行身份验证
- 在尝试检索经过身份验证的Google+用户的电子邮件地址时获取“gapi.client未定义”
- 如何在选中数组复选框的情况下获取数组文本框的验证
- 如何在表单验证时获取函数的返回值
- 仅在 Tag-it 中为获取的数据创建标记.js希望验证任何外部数据的创建标记
- 如何在客户端javascriptCode中获取验证器ClientID
- 从经过身份验证的用户获取打开图形操作 ID
- Struts 1.3 将动态创建的值获取到后端进行验证
- 按必需属性验证输入元素,并获取每个输入的标签文本
- 当验证在 laravel 5 中返回 false 时,如何获取选项值的名称
- 使浏览器重新验证(但不一定重新获取)图像
- 使用输入类获取已验证输入的父级
- 借助 Jquery 验证插件获取图像宽度
- 在 JS 中的表单验证方法中获取表单提交方法的输出