简体   繁体   中英

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. I want to use the submit button because it triggers HTML5's form validations where form.submit() would not. 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

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.

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.

Test Code:

jsfiddle link: 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; }

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" );
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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