我如何使用 jqueryui 对话框按钮提交表单,

新手上路,请多包涵

我想使用 jqueryui 按钮提交一个表单。我有一些代码,但它不起作用。这是代码:

 <script type="text/javascript">
function findUrls()
{
    var text = document.getElementById("text").value;
    var source = (text || '').toString();
    var urlArray = [];
    var url;
    var matchArray;

    // Regular expression to find FTP, HTTP(S) and email URLs.
    var regexToken = /\b(http|https)?(:\/\/)?(\S*)\.(\w{2,4})\b/ig;

    // Iterate through any URLs in the text.
    if( (regexToken.exec( source )) !== null )
    {
    show_box();// this will show jquery dialog..
    return false;
    }

}

</script>

<div id="dialog" title="Dialog Title">
<p>Dialog box text.....Dialog box text....Dialog box text</p>
<button id="formSubmit">Click me</button>
</div>

<form name="myForm" id="myForm" action="http://www.bing.com" method="post" onsubmit="return findUrls();">
<textarea id="text"></textarea>
<input type="submit" name="submit" value="send" />
</form>

<script type="text/javascript">
function show_box(){
$(document).ready(function(){

                $( "#dialog" ).dialog({
                autoOpen: false,
                width: 400,
                buttons: [
                    {
                        text: "Yes",
                        click: function() {
                            submit_form();

                        }
                        },
                    {
                        text: "No",
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    },
                    {
                        text: "Cancel",
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                ]
            });

            $( "#dialog" ).dialog( "open" );
});
}

function submi_form(){
var myForm = document.forms['myForm'];

var formSubmit = document.getElementById('formSubmit');

formSubmit.onclick = function(){
    myForm.submit();
    }
}
</script>

当一个人在文本区域中放置一个链接并提交表单时,会出现带有三个按钮的 jQuery 对话框,我希望当有人单击对话框上的 按钮时,表单会自动提交。一切正常,但是当我单击“是”按钮时,它不起作用。

原文由 Gopa Soft 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 240
2 个回答

您的 submit_form 函数实际上并没有尝试提交表单。它目前正在“点击我”按钮上添加一个点击事件,如果按下该按钮将提交您的表单。

如果您想单击对话框的“是”按钮提交表单,请尝试以下操作:

 function submit_form(){
    $('#myForm').submit();
}

还要确保您的 submi_form 方法的名称只是一个拼写错误,而不是在您的实时代码中……您缺少一个“t”。

原文由 purgatory101 发布,翻译遵循 CC BY-SA 3.0 许可协议

好吧,首先所有的代码都非常混乱,而且组织得非常糟糕,很抱歉这么说。一些改进建议:

1:将javascirpt放在单独的js文件中,尽量避免嵌入代码。但这并不意味着完全不使用嵌入。在这种情况下,最好让它更有条理。

2:确保将加载事件调用到需要它的代码中,在您的情况下,在 show_box() 内部使用了 document ready,这是完全没有必要的。

3: 函数 submi_form() 不会从任何地方调用,所以此时你最好使用 document ready 来处理点击事件

4:findUrls(),很抱歉在这部分无法得到你想要完成的,所以绕过它。确保你保持简单,幸福就会到来)

试图修复代码,使您的风格尽可能接近,所以我们在这里 http://jsbin.com/idetub/1/edit

仅供您考虑的 javascript

 function findUrls() {
  show_box(); // this will show jquery dialog..
  return false;

  // some broken logic below ...
  var text = document.getElementById("text").value;
  var source = (text || '').toString();
  var urlArray = [];
  var url;
  var matchArray;
  // Regular expression to find FTP, HTTP(S) and email URLs.
  var regexToken = /\b(http|https)?(:\/\/)?(\S*)\.(\w{2,4})\b/ig;
  // Iterate through any URLs in the text.
  if ((regexToken.exec(source)) !== null) {
    show_box(); // this will show jquery dialog..
    return false;
  }
}

 function show_box() {
   //$(document).ready(function(){ // no needs for that
   $("#dialog").dialog({
     autoOpen: false,
     width: 400,
     buttons: [{
       text: "Yes",
       click: function () {
         submit_form();
       }
     }, {
       text: "No",
       click: function () {
         $(this).dialog("close");
       }
     }, {
       text: "Cancel",
       click: function () {
         $(this).dialog("close");
       }
     }]
   });
   $("#dialog").dialog("open");
   //});
 }
$(document).ready(function(){
  //function submi_form() { remove this as no accual call for submit_form nowhere
    var myForm = document.forms['myForm'];
    var formSubmit = document.getElementById('formSubmit');
    formSubmit.onclick = function () {
      myForm.submit();
    };
  //}
});

UPD:是的,看起来你在这里得到了递归逻辑,它无法提交表单。

原文由 dmi3y 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题