Javascript 我们如何检查在jQuery UI对话框中单击了哪个按钮?

Javascript 我们如何检查在jQuery UI对话框中单击了哪个按钮?,javascript,html,jquery-ui,Javascript,Html,Jquery Ui,我正在尝试使用jqueryui构建一个对话框,其中包含用于用户确认的Yes/No按钮。(这是因为我想使UI统一,因为我已经使用jQuery UI构建了警告对话框。)我的目的是要求用户确认是否在文本框中提交了大量(1000或以上)。到目前为止,我的JavaScript代码如下所示: function checkclick(button1, button2, theForm) { var val; val = theForm.mybox.value; v = parseInt(va

我正在尝试使用jqueryui构建一个对话框,其中包含用于用户确认的Yes/No按钮。(这是因为我想使UI统一,因为我已经使用jQuery UI构建了警告对话框。)我的目的是要求用户确认是否在文本框中提交了大量(1000或以上)。到目前为止,我的JavaScript代码如下所示:

function checkclick(button1, button2, theForm) {
   var val;
   val = theForm.mybox.value;
   v = parseInt(val) || 0;
   var btns = {};
   btns[button1] = function(){ 
      $(this).dialog('close');
   };
   btns[button2] = function(){ 
      $(this).dialog('close');
   };

   if (v >= 1000) {
      $('#dialog').dialog({
         autoOpen: true,
         modal:true,
         buttons:btns
      }); 
      $('#dialog_link').click(function () {
          $('#dialog').dialog('open');
      });
      return false;
   }
   return true;
}
<div id='dialog' title='Note' style='display:none'>
<p id='dialog_link'>This is a very large number. Are you sure?</p>
</div>

<form name='myForm' action='result.php' method='post' 
 onsubmit="return checkclick('Yes', 'No', this)">
<input type='text' name='mybox'>
<input type='submit'>
</form>
<form id="target" action="destination.html">
  <input type="text" value="string value">
  <input type="submit" value="Submit">
</form>

$( "#target" ).submit(function( event ) {
  if (//result of dialog is false) {
    event.preventDefault();
  } else {
    return;
  }  
});
我的HTML如下所示:

function checkclick(button1, button2, theForm) {
   var val;
   val = theForm.mybox.value;
   v = parseInt(val) || 0;
   var btns = {};
   btns[button1] = function(){ 
      $(this).dialog('close');
   };
   btns[button2] = function(){ 
      $(this).dialog('close');
   };

   if (v >= 1000) {
      $('#dialog').dialog({
         autoOpen: true,
         modal:true,
         buttons:btns
      }); 
      $('#dialog_link').click(function () {
          $('#dialog').dialog('open');
      });
      return false;
   }
   return true;
}
<div id='dialog' title='Note' style='display:none'>
<p id='dialog_link'>This is a very large number. Are you sure?</p>
</div>

<form name='myForm' action='result.php' method='post' 
 onsubmit="return checkclick('Yes', 'No', this)">
<input type='text' name='mybox'>
<input type='submit'>
</form>
<form id="target" action="destination.html">
  <input type="text" value="string value">
  <input type="submit" value="Submit">
</form>

$( "#target" ).submit(function( event ) {
  if (//result of dialog is false) {
    event.preventDefault();
  } else {
    return;
  }  
});

这是一个非常大的数字。你确定吗


问题是,当用户单击“是”或“否”按钮时,它将返回到同一页面。但是,如果我在“if”部分中将“return false”更改为“return true”,那么一旦单击提交按钮,页面将直接转到result.php,而无需等待用户单击是或否按钮。是否有任何方法可以检查用户正在单击哪个按钮,以便在单击“是”后页面将转到result.php,但在单击“否”后仍保持在当前页面?

jQuery对话框有一个选项
buttons
,可用于描述所需的按钮及其操作

function checkclick(button1, button2, theForm) {
   var val;
   val = theForm.mybox.value;
   v = parseInt(val) || 0;

   if (v >= 1000) {
      $('#dialog').dialog({
         autoOpen: true,
         modal:true,
         buttons:{
            "Yes":function() {
               alert('Yes has been clicked'); //Your coding here
            },
            "No": function() {
               $( this ).dialog( "close" );
            }
         }
      }); 
      $('#dialog_link').click(function () {
          $('#dialog').dialog('open');
      });
      return false;
   }
   return true;
}
我没有“声誉”来评论你的后续行动,所以我不得不发帖作为回答。对违反协议表示歉意

如果我正确理解你的目标,你只需要有条件地提交表格。我相信,如果用户认为表单提交太长,您可以通过防止默认行为来实现这一点。大概是这样的:

function checkclick(button1, button2, theForm) {
   var val;
   val = theForm.mybox.value;
   v = parseInt(val) || 0;
   var btns = {};
   btns[button1] = function(){ 
      $(this).dialog('close');
   };
   btns[button2] = function(){ 
      $(this).dialog('close');
   };

   if (v >= 1000) {
      $('#dialog').dialog({
         autoOpen: true,
         modal:true,
         buttons:btns
      }); 
      $('#dialog_link').click(function () {
          $('#dialog').dialog('open');
      });
      return false;
   }
   return true;
}
<div id='dialog' title='Note' style='display:none'>
<p id='dialog_link'>This is a very large number. Are you sure?</p>
</div>

<form name='myForm' action='result.php' method='post' 
 onsubmit="return checkclick('Yes', 'No', this)">
<input type='text' name='mybox'>
<input type='submit'>
</form>
<form id="target" action="destination.html">
  <input type="text" value="string value">
  <input type="submit" value="Submit">
</form>

$( "#target" ).submit(function( event ) {
  if (//result of dialog is false) {
    event.preventDefault();
  } else {
    return;
  }  
});

$(“#目标”)。提交(功能(事件){
if(//对话框的结果为false){
event.preventDefault();
}否则{
返回;
}  
});

您的问题大部分在这里得到了回答:@JoelPrz谢谢,是的,解决方案解决了我的问题。唯一的区别是我需要替换
window.open(…)form.submit()对>进行编码
这样表单数据就可以传递了。我相信你上面的代码,在
if
之前涉及
BTN
的行可以被删除。我明白你的意思了,这个解决方案是不允许提交用户输入的大量数据。我想要的是请求用户确认,如果用户单击Yes,则仍将提交大量数据。事实上,你的评论中包含的内容已经足够好了。