Jquery验证不适用于我的ASP.NET母版页 [英] Jquery validation not working with my ASP.NET master page
问题描述
我正在尝试在使用母版页的asp.net页面上使用jQuery验证。每当我使用没有母版页的代码时,它工作正常。但是使用母版页它不起作用。 2之间唯一不同的是,如果没有母版页,则在呈现网页时,组名称保持不变。 name =Game_1,主页面更改为name =ctl00 $ ContentPlaceHolder1 $ Game_1
我试图在我的jQuery中使用上述两个名称但是它仍然无法正常工作。我也尝试了两种不同的jQuery代码,但它们都不起作用。
另外,我的收音机按钮位于中继器内。在我的网站上,我有17对单选按钮,但不认为有必要将所有代码放在这里。
任何帮助将不胜感激。谢谢
我尝试过:
< asp:RadioButton ID =radBearsrunat =serverText =BearsGroupName =Game_1CssClass =rnd/>
< asp:RadioButton ID =radRaidersrunat =serverText =RaidersGroupName =Game_1CssClass =rnd/>
< asp:CustomValidator ID =CustomValidator1runat =serverErrorMessage =需要选择ClientValidationFunction =ValidateRadioButton1/>
< asp:RadioButton ID =radChargersrunat =serverText =ChargersGroupName =Game_2CssClass =rnd/>
< asp: RadioButton ID =radCowboysrunat =serverText =CowboysGroupName =Game_2CssClass =rnd/>
< asp:CustomValidator ID =CustomValidator2runat =serverErrorMessage =需要选择ClientValidationFunction =ValidateRadioButton2/>
< asp:Button ID =Button1runat =serverText =Submit/>
< script src =Scripts / jquery-3.3.1.min.js>
函数ValidateRadioButton1(sender,args){
args.IsValid = $([name = Game_1]:选中)。长度> 0;
}
函数ValidateRadioButton2(sender,args){
args.IsValid = $( [name = Game_2]:选中)。长度> 0;
}
我也试过这个:
函数ValidateRadioButton(sender,args){
var repeater = document.getElementById(<%= myRepeater.ClientID%>);
var items = repeater.getElementsByTagName('input');
for(var i = 0; i< items.length; i ++){
if(items [i] .type ==radio){
if(items [i] .checked){
args.IsValid = true;
return;
}
else {
args.IsValid = false;
}
}
}
}
ContentPlaceHolder1
Game_1
我尝试在我的jQuery中使用上面的这两个名称,但它仍然不起作用。我也试过2不同的jQuery代码,但它们都不起作用。
另外,我的收音机按钮位于中继器内。在我的网站上,我有17对单选按钮,但不认为有必要将所有代码放在这里。
任何帮助将不胜感激。谢谢
我尝试过:
< asp:RadioButton ID =radBearsrunat =serverText =BearsGroupName =Game_1CssClass =rnd/>
< asp:RadioButton ID =radRaidersrunat =serverText =RaidersGroupName =Game_1CssClass =rnd/>
< asp:CustomValidator ID =CustomValidator1runat =serverErrorMessage =需要选择ClientValidationFunction =ValidateRadioButton1/>
< asp:RadioButton ID =radChargersrunat =serverText =ChargersGroupName =Game_2CssClass =rnd/>
< asp: RadioButton ID =radCowboysrunat =serverText =CowboysGroupName =Game_2CssClass =rnd/>
< asp:CustomValidator ID =CustomValidator2runat =serverErrorMessage =需要选择ClientValidationFunction =ValidateRadioButton2/>
< asp:Button ID =Button1runat =serverText =Submit/>
< script src =Scripts / jquery-3.3.1.min.js>
函数ValidateRadioButton1(sender,args){
args.IsValid =
(\"[name=Game_1]:checked\").length> 0;
}
函数ValidateRadioButton2(sender,args){
args.IsValid =
I'm trying to use jQuery validation on my asp.net page that uses a master page. Whenever I use this code without the master page it works fine. But with the master page it doesn't work. The only thing different between the 2 is that without the master page the Group Name stays the same when web page is rendered. name="Game_1" with the master page it changes to name="ctl00$ContentPlaceHolder1$Game_1"
I tried to use both of those names above in my jQuery but it still doesn't work. I also tried 2 different jQuery codes as well but neither of them work.
Also, my Radio Buttons are inside a Repeater. On my site I have 17 pairs of radio button but didn't think it was necessary to put all that code here.
Any help would be appreciated. Thanks
What I have tried:
<asp:RadioButton ID="radBears" runat="server" Text="Bears" GroupName="Game_1" CssClass="rnd" />
<asp:RadioButton ID="radRaiders" runat="server" Text="Raiders" GroupName="Game_1" CssClass="rnd" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Selection is required" ClientValidationFunction = "ValidateRadioButton1" />
<asp:RadioButton ID="radChargers" runat="server" Text="Chargers" GroupName="Game_2" CssClass="rnd" />
<asp:RadioButton ID="radCowboys" runat="server" Text="Cowboys" GroupName="Game_2" CssClass="rnd" />
<asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="Selection is required" ClientValidationFunction = "ValidateRadioButton2" />
<asp:Button ID="Button1" runat="server" Text="Submit" />
<script src="Scripts/jquery-3.3.1.min.js">
function ValidateRadioButton1(sender, args) {
args.IsValid = $("[name=Game_1]:checked").length > 0;
}
function ValidateRadioButton2(sender, args) {
args.IsValid = $("[name=Game_2]:checked").length > 0;
}
I also tried this:
function ValidateRadioButton(sender, args) {
var repeater = document.getElementById("<%= myRepeater.ClientID %>");
var items = repeater.getElementsByTagName('input');
for (var i = 0; i < items.length; i++) {
if (items[i].type == "radio") {
if (items[i].checked) {
args.IsValid = true;
return;
}
else {
args.IsValid = false;
}
}
}
}
ContentPlaceHolder1
Game_1"
I tried to use both of those names above in my jQuery but it still doesn't work. I also tried 2 different jQuery codes as well but neither of them work.
Also, my Radio Buttons are inside a Repeater. On my site I have 17 pairs of radio button but didn't think it was necessary to put all that code here.
Any help would be appreciated. Thanks
What I have tried:
<asp:RadioButton ID="radBears" runat="server" Text="Bears" GroupName="Game_1" CssClass="rnd" />
<asp:RadioButton ID="radRaiders" runat="server" Text="Raiders" GroupName="Game_1" CssClass="rnd" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Selection is required" ClientValidationFunction = "ValidateRadioButton1" />
<asp:RadioButton ID="radChargers" runat="server" Text="Chargers" GroupName="Game_2" CssClass="rnd" />
<asp:RadioButton ID="radCowboys" runat="server" Text="Cowboys" GroupName="Game_2" CssClass="rnd" />
<asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="Selection is required" ClientValidationFunction = "ValidateRadioButton2" />
<asp:Button ID="Button1" runat="server" Text="Submit" />
<script src="Scripts/jquery-3.3.1.min.js">
function ValidateRadioButton1(sender, args) {
args.IsValid =
("[name=Game_1]:checked").length > 0;
}
function ValidateRadioButton2(sender, args) {
args.IsValid =
这篇关于Jquery验证不适用于我的ASP.NET母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!