繁体   English   中英

将Jquery UI对话框按钮更改为表单提交按钮

[英]Changing Jquery UI Dialog Button to a form submit button

我正在尝试使用Jquery-ui对话框按钮之一作为表单提交按钮。 我想使用Submit按钮,因为它触发了HTML5的表单验证,而form.submit()不会。 目前,我正在使用一个隐藏的提交按钮,并使用一个点击触发器来提交表单,但是如果我能使它正常工作,它将变得更加整洁。

我可以毫无疑问地将按钮更改为类型submit ,但似乎无法将form添加到属性中

由于按钮不在表单标签内,因此无法正确提交

期望:

<button type="submit" form='createUser' id="submit-button" everythingelse="works" foo="bar" class="ui-button ui-corner-all ui-widget">submit</button>

结果:

<button type="submit" id="submit-button" everythingelse="works" foo="bar" class="ui-button ui-corner-all ui-widget">submit</button>

我可以为按钮设置各种随机的东西,但是由于某种原因它不会采取form ,我只是不明白为什么。

我读了博客在这里 ,它似乎form使用在以前版本的jQuery的UI的工作,但它不工作了。

测试代码:

jsfiddle链接: https ://jsfiddle.net/s5cjk3wr/

的HTML:

<div id="dialog-form" title="Create new user">

  <form id='createUser'>
    <fieldset>
      <label for="name">Name</label>
      <input type="text" name="name" id="name" value="Jane Smith" class="text ui-widget-content ui-corner-all">
      <label for="email">Email</label>
      <input type="text" name="email" id="email" value="jane@smith.com" class="text ui-widget-content ui-corner-all">
      <label for="password">Password</label>
      <input type="password" name="password" id="password" value="xxxxxxx" class="text ui-widget-content ui-corner-all">

      <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
</div>

<button id="create-user">Create new user</button>

javascript:

 $( function() {
      dialog = $( "#dialog-form" ).dialog({
        autoOpen: false,
        height: 400,
        width: 350,
        modal: true,
        buttons: {
          "Create an account": { 
            text:"submit", 
            id: "submit-button",
            type: "submit", 
            form: "createUser",
            everythingElse: "works",
            foo: "bar"
          },
          Cancel: function() {
            dialog.dialog( "close" );
          }
        }
      });

      form = dialog.find( "form" ).on( "submit", function( event ) {
        event.preventDefault();
      });

      $( "#create-user" ).button().on( "click", function() {
        dialog.dialog( "open" );
      });
 });

的CSS

label, input { display:block; }
input.text { margin-bottom:12px; width:95%; padding: .4em; }
fieldset { padding:0; border:0; margin-top:25px; }
h1 { font-size: 1.2em; margin: .6em 0; }
div#users-contain { width: 350px; margin: 20px 0; }
div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
.ui-dialog .ui-state-error { padding: .3em; }
.validateTips { border: 1px solid transparent; padding: 0.3em; }

为什么不附加单击时将提交表单的功能,就像这样。

buttons: {
      "Create an account": function(){$('#createUser').submit()},
      Cancel: function() {
        dialog.dialog( "close" );
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM