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

时间:2012-12-04 16:45:13

标签: javascript jquery html jquery-ui javascript-events

我想使用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对话框会出现三个按钮,我希望当有人单击对话框中的“是”按钮时,表单会自动提交。一切都很好。但当我点击按钮是,它不起作用。

3 个答案:

答案 0 :(得分:6)

您的submit_form函数实际上并未尝试提交表单。它目前在“Click Me”按钮上添加了一个点击事件,如果按下该按钮,则会提交您的表单。

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

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

另外,请确保您的submi_form方法的名称只是一个拼写错误而不是您的实时代码......您错过了“t”。

答案 1 :(得分:2)

好吧,所有代码中的第一个都非常纠结,组织得非常糟糕,抱歉这样说。一些改进的提示:

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

2:确保将load事件调用到需要它的代码中,在你的情况下,在show_box()中使用了文档,这是完全没必要的。

3:函数submi_form()不是从任何地方调用的,所以此时你可以更好地使用document ready来处理click事件

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:是的,看起来你在这里得到了递归逻辑,无法提交表格。

答案 2 :(得分:0)

试试这个:

$("#myForm").submit(function() {

    var text = $('#text').val;
    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..
        alert('ss');
        return false;
    }
    return false;
});​

同时从表单中删除onsubmit="return findUrls();",并在上面的findUrls()方法中添加$("#myForm").submit()代码。 Fiddle