简体   繁体   English

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

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

I'm trying to use one of the Jquery-ui Dialog buttons as a form submit button. 我正在尝试使用Jquery-ui对话框按钮之一作为表单提交按钮。 I want to use the submit button because it triggers HTML5's form validations where form.submit() would not. 我想使用Submit按钮,因为它触发了HTML5的表单验证,而form.submit()不会。 Currently, I'm using a hidden submit button and using a click trigger to submit the form, but it would be alot neater if I can get this to work. 目前,我正在使用一个隐藏的提交按钮,并使用一个点击触发器来提交表单,但是如果我能使它正常工作,它将变得更加整洁。

I can change the button to a type submit with no problem, but I can't seem to add form to the attributes 我可以毫无疑问地将按钮更改为类型submit ,但似乎无法将form添加到属性中

Since the button is not within the form tags I can't get it to submit correctly 由于按钮不在表单标签内,因此无法正确提交

Expecting: 期望:

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

Result: 结果:

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

I can set all kind of random things to the button, but it will not take take form for some reason, I just can't understand why. 我可以为按钮设置各种随机的东西,但是由于某种原因它不会采取form ,我只是不明白为什么。

I've read a blog here , and it seems form used to work in a previous version of jquery-ui but it doesn't work anymore. 我读了博客在这里 ,它似乎form使用在以前版本的jQuery的UI的工作,但它不工作了。

Test Code: 测试代码:

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

html: 的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: 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 的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; }

Why don't you attach function which will submit the form when you click, something like this. 为什么不附加单击时将提交表单的功能,就像这样。

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