Javascript 仅当.aspx表单上存在某些条件时,才会引发jQuery UI确认对话框
我在让jQueryUI确认对话框按我需要的方式工作时遇到问题。我在Visual Studio中有一个.aspx表单,后面有C#代码。我想要求用户确认submit按钮,但前提是RadioButtonList控件中选择了特定的单选按钮,且整数值(代码后面的公共int属性)大于0 以下是我的确认对话框的代码:Javascript 仅当.aspx表单上存在某些条件时,才会引发jQuery UI确认对话框,javascript,asp.net,jquery-ui,Javascript,Asp.net,Jquery Ui,我在让jQueryUI确认对话框按我需要的方式工作时遇到问题。我在Visual Studio中有一个.aspx表单,后面有C#代码。我想要求用户确认submit按钮,但前提是RadioButtonList控件中选择了特定的单选按钮,且整数值(代码后面的公共int属性)大于0 以下是我的确认对话框的代码: //Confirm Replace Bill Info dialog var replConfirmed = false; function ConfirmReplaceDialog(obj,
//Confirm Replace Bill Info dialog
var replConfirmed = false;
function ConfirmReplaceDialog(obj, title, dialogText) {
var rbl = document.getElementsByName("<%= rbReplaceAppend.ClientID %>");
var infoID = parseInt("<%= infoID %>");
//Raise this dialog only if "Replace" is selected and there is existing info
if (rbl[0].checked && infoID > 0) {
if (!replConfirmed) {
//add the dialog div to the page
$('body').append(String.Format("<div id='confirmReplaceDialog' title='{0}'><p>{1}</p></div>", title, dialogText));
//create the dialog
$('#confirmReplaceDialog').dialog({
modal: true,
resizable: false,
draggable: false,
close: function(event, ui) {
$('body').find('#confirmReplaceDialog').remove();
},
buttons: {
'Yes, replace info': function() {
$(this).dialog('close');
replConfirmed = true;
if (obj) obj.click();
},
'No, cancel': function() {
$(this).dialog('close');
}
}
});
}
return replConfirmed;
}
//if "Replace" is not selected (i.e. "Append" is selected), always return true
return true;
}
//确认替换票据信息对话框
var=false;
函数ConfirmReplaceDialog(对象、标题、对话框文本){
var rbl=document.getElementsByName(“”);
var infoID=parseInt(“”);
//仅当选择了“替换”且存在现有信息时,才会引发此对话框
如果(rbl[0]。选中&&infoID>0){
如果(!已确认){
//将对话框div添加到页面中
$('body').append(String.Format(“{1}”,title,dialogText));
//创建对话框
$('confirmReplaceDialog')。对话框({
莫代尔:是的,
可调整大小:false,
可拖动:错误,
关闭:函数(事件,ui){
$('body').find('confirmReplaceDialog').remove();
},
按钮:{
“是,替换信息”:函数(){
$(this.dialog('close');
回复确认=正确;
如果(obj)obj.click();
},
“否,取消”:函数(){
$(this.dialog('close');
}
}
});
}
返回已确认的回复;
}
//如果未选择“替换”(即选择了“附加”),则始终返回true
返回true;
}
下面是.aspx页面的一部分(太长了,无法发布整个内容),应该调用该对话框:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:RadioButtonList ID="rbReplaceAppend" runat="server" Font-Size="X-Small" RepeatDirection="Horizontal">
<asp:ListItem Selected="True">Replace</asp:ListItem>
<asp:ListItem>Append</asp:ListItem>
</asp:RadioButtonList>
</td>
<td rowspan="2">
<asp:Label ID="lblReplaceAppend" runat="server" CssClass="instructions" Text="(select 'Replace' to replace the information here with the Bill Request info, or 'Append' to add the Bill Request info to information already present)"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnGetReqInfo" runat="server" Text="Get Request Info" OnClick="btnGetReqInfo_Click" OnClientClick="return ConfirmReplaceDialog(this, 'Please confirm information replacement', 'Warning: you are replacing existing information. Are you sure you wish to do this (cannot be undone)?');" />
</td>
</tr>
</table>
代替
追加
发生的事情是,我的对话框从未被引发——OnClick事件总是激发并提交表单
有什么想法吗
更新(根据要求):
以下是浏览器看到的代码部分:
Javascript:
//Confirm Replace Bill Info dialog
var replConfirmed = false;
function ConfirmReplaceDialog(obj, title, dialogText) {
var rbl = document.getElementsByName('ctl00_mainContent_rblReplaceAppend');
var infoID = parseInt('10');
//Raise this dialog only if "Replace" is selected and there is existing info
if (rbl[0].checked && infoID > 0) {
alert('conditions exist for confirmation');
if (!replConfirmed) {
//add the dialog div to the page
$('body').append(String.Format("<div id='confirmReplaceDialog' title='{0}'><p>{1}</p></div>", title, dialogText));
//create the dialog
$('#confirmReplaceDialog').dialog({
modal: true,
resizable: false,
draggable: false,
close: function(event, ui) {
$('body').find('#confirmReplaceDialog').remove();
},
buttons: {
'Yes, replace info': function() {
$(this).dialog('close');
replConfirmed = true;
if (obj) obj.click();
},
'No, cancel': function() {
$(this).dialog('close');
}
}
});
}
return replConfirmed;
}
//if "Replace" is not selected (i.e. "Append" is selected), always return true
return true;
}
//确认替换票据信息对话框
var=false;
函数ConfirmReplaceDialog(对象、标题、对话框文本){
var rbl=document.getElementsByName('ctl00_main content_rblreplacepend');
var infoID=parseInt('10');
//仅当选择了“替换”且存在现有信息时,才会引发此对话框
如果(rbl[0]。选中&&infoID>0){
警报(“存在确认条件”);
如果(!已确认){
//将对话框div添加到页面中
$('body').append(String.Format(“{1}”,title,dialogText));
//创建对话框
$('confirmReplaceDialog')。对话框({
莫代尔:是的,
可调整大小:false,
可拖动:错误,
关闭:函数(事件,ui){
$('body').find('confirmReplaceDialog').remove();
},
按钮:{
“是,替换信息”:函数(){
$(this.dialog('close');
回复确认=正确;
如果(obj)obj.click();
},
“否,取消”:函数(){
$(this.dialog('close');
}
}
});
}
返回已确认的回复;
}
//如果未选择“替换”(即选择了“附加”),则始终返回true
返回true;
}
.aspx:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table id="ctl00_mainContent_rblReplaceAppend" border="0" style="font-size:X-Small;">
<tr>
<td><input id="ctl00_mainContent_rblReplaceAppend_0" type="radio" name="ctl00$mainContent$rblReplaceAppend" value="Replace" checked="checked" /><label for="ctl00_mainContent_rblReplaceAppend_0">Replace</label></td>
<td><input id="ctl00_mainContent_rblReplaceAppend_1" type="radio" name="ctl00$mainContent$rblReplaceAppend" value="Append" /><label for="ctl00_mainContent_rblReplaceAppend_1">Append</label></td>
</tr>
</table>
</td>
<td rowspan="2">
<span id="ctl00_mainContent_lblReplaceAppend" class="instructions">(select 'Replace' to replace the information here with the Bill Request info, or 'Append' to add the Bill Request info to information already present)</span>
</td>
</tr>
<tr>
<td>
<input type="submit" name="ctl00$mainContent$btnGetReqInfo" value="Get Request Info" onclick="return ConfirmReplaceDialog(this, 'Please confirm information replacement', 'Warning: you are replacing existing information. Are you sure you wish to do this (cannot be undone)?');WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$mainContent$btnGetReqInfo", "", true, "valGetRequest", "", false, false))" id="ctl00_mainContent_btnGetReqInfo" />
</td>
</tr>
</table>
代替
追加
(选择“替换”将此处的信息替换为票据申请信息,或选择“附加”将票据申请信息添加到现有信息中)
我刚刚找到了答案-无论出于何种原因,我必须在这里使用UniqueID而不是ClientID(我需要了解两者的区别,以及何时使用它们是合适的)。将document.getElementsByName(“”)
替换为document.getElementsByName(“”)
后,它工作正常。我刚刚想到打开Chrome的Javascript调试器,看看是否有任何问题。。。还有。我得到了Uncaught类型错误:无法读取if(rbl[0]。checked&&infoID>0){
行中未定义的属性“checked”。这告诉我Javascript没有找到我的RadioButtonList。不是文档。getElementsByName(“”)
正确的方法?什么是JQuery验证?我以前从未听说过。看看浏览器实际看到的源代码会很好。你似乎以一种我认为不起作用的方式混合了C#和javascript。@tzerb我只是读了一点关于JQuery验证插件的内容-我看不出它在这种情况下有什么帮助我不想在这里验证表单条目。