javascript - 将 Jquery UI 对话框按钮更改为表单提交按钮
问题描述
我正在尝试使用其中一个 Jquery-ui 对话框按钮作为表单提交按钮。我想使用提交按钮,因为它会触发 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" );
}
推荐阅读
- javascript - 根据第一列中选择的内容启用第二列中的单选按钮
- azure-webjobs - Azure LogicApps 可以停止网络作业吗?
- sql - 如何在临时 DataTable 中执行循环并在比较两个表之间的值后更新 SQL 表
- android - 在平面图中上下滑动
- firebase - Firebase Cloud功能可以靠近地方吗?
- autoit - 无法运行基本示例 autoit 脚本
- python - 将 Pandas DataFrame 附加到现有 Excel 文档
- passenger - 如何在独立模式下适当地设置passenger_base_uri
- angular - Spring boot 控制器 - 上传 Multipart 文件和 Java 对象,Angular 4,7
- python - 如何从用户输入到字典中跟踪多个类实例化