【发布时间】:2013-03-04 12:03:09
【问题描述】:
在我的页面上,我有一个单选按钮列表和一个文本框。
默认情况下,文本框是隐藏的,如果用户单击“是”,则会显示该文本框。一旦他们单击“是”,则必须先填写文本框,然后才能继续。
我已经输入了以下代码,但它似乎根本不起作用,当按下按钮转到下一页时,它会继续而不是停止并要求用户在框中输入内容。
function Q12iYes() {
document.getElementById('txt12i').style.display = "block"
}
function Q12iNo() {
document.getElementById('txt12i').style.display = "none"
}
<asp:RadioButtonList ID="rbtn12" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="Yes" onclick="Q12iYes()" /> <asp:ListItem Value="No" Selected="True" onclick="Q12iNo()" />
</asp:RadioButtonList>
<br />
12.i) If 'Yes' please provide details<br />
<br />
<asp:TextBox ID="txt12i" runat="server" Height="62px" TextMode="MultiLine" Width="343px"></asp:TextBox>
<br />
<asp:CustomValidator ID="MyTxtBoxValidator" runat="server" ControlToValidate="txt12i" ValidateEmptyText="true" ErrorMessage="Please enter some values into Textbox"
ClientValidationFunction="validateMyBtn" Display="Dynamic"> </asp:CustomValidator>
<script type="text/javascript">
function validateMyBtn(oSrc, args) {
var rbtnValue = null;
var rbtnList = document.getElementsByName('<%=rbtn12.ClientID %>');
var radio = rbtnList[0].getElementsByTagName("input");
for (var i = 0; i < radio.length; i++) { if (radio[i].checked) rbtnValue = radio[i].value; }
if (rbtnValue == "Yes") { args.IsValid = !(args.Value == "") }
else { args.IsValid = true; }
}
</script>
<asp:Button ID="btnContinue" runat="server" OnClick="btnContinue_Click" Text="Continue" />
我已经查看了如何执行此操作,但我所看到的也没有任何效果,或者帮助我了解我哪里出错了。
谁能帮忙?
【问题讨论】:
-
您需要自定义验证器吗?为什么不使用必填字段验证器?如果输入框不可见,则必填字段验证器不会尝试验证。
-
从我所有的搜索来看,自定义验证器似乎是我需要的,因为我使用 javascript 在用户选择时显示和隐藏文本框。我正在努力寻找最有效的方法。
-
你会用jQuery吗?
标签: c# asp.net textbox radiobuttonlist customvalidator