Jquery Modal/Dialog表单不起作用
Jquery Modal/Dialog form not working
嗨,我刚开始编码&尝试使用Jquery库Dialog/Modal表单。我已经准备好了所有的CSS、JS和HTML,但当我点击"创建新用户"时,对话框/Modal不会出现(按钮后面的功能不起作用)。我在控制台中得到了以下错误-任何帮助都将不胜感激:
控制台中的错误消息:
Uncaught TypeError: undefined is not a function main.js
and it points to this ---> dialog = $( "#dialog-form" ).dialog({
main.js文件
$(function() {
var dialog, form,
// From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29
emailRegex = /^[a-zA-Z0-9.!#$%&'*+'/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:'.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
name = $( "#name" ),
email = $( "#email" ),
password = $( "#password" ),
allFields = $( [] ).add( name ).add( email ).add( password ),
tips = $( ".validateTips" );
function updateTips( t ) {
tips
.text( t )
.addClass( "ui-state-highlight" );
setTimeout(function() {
tips.removeClass( "ui-state-highlight", 1500 );
}, 500 );
}
function checkLength( o, n, min, max ) {
if ( o.val().length > max || o.val().length < min ) {
o.addClass( "ui-state-error" );
updateTips( "Length of " + n + " must be between " +
min + " and " + max + "." );
return false;
} else {
return true;
}
}
function checkRegexp( o, regexp, n ) {
if ( !( regexp.test( o.val() ) ) ) {
o.addClass( "ui-state-error" );
updateTips( n );
return false;
} else {
return true;
}
}
function addUser() {
var valid = true;
allFields.removeClass( "ui-state-error" );
valid = valid && checkLength( name, "username", 3, 16 );
valid = valid && checkLength( email, "email", 6, 80 );
valid = valid && checkLength( password, "password", 5, 16 );
valid = valid && checkRegexp( name, /^[a-z]([0-9a-z_'s])+$/i, "Username may consist of a-z, 0-9, underscores, spaces and must begin with a letter." );
valid = valid && checkRegexp( email, emailRegex, "eg. ui@jquery.com" );
valid = valid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if ( valid ) {
$( "#users tbody" ).append( "<tr>" +
"<td>" + name.val() + "</td>" +
"<td>" + email.val() + "</td>" +
"<td>" + password.val() + "</td>" +
"</tr>" );
dialog.dialog( "close" );
}
return valid;
}
dialog = $( "#dialog-form" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Create an account": addUser,
Cancel: function() {
dialog.dialog( "close" );
}
},
close: function() {
form[ 0 ].reset();
allFields.removeClass( "ui-state-error" );
}
});
form = dialog.find( "form" ).on( "submit", function( event ) {
event.preventDefault();
addUser();
});
$( "#create-user" ).button().on( "click", function() {
dialog.dialog( "open" );
});
});
index.html文件
<html>
<head>
<title>Modal form</title>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" />
<link rel="stylesheet" type="text/css" media="all" href="css/grid.css" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css" />
<link href='http://fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>
<script src="js/jquery-1.11.2.js"></script>
<script src="js/main.js"></script>
</head>
<body>
<div id="dialog-form" title="Create new user">
<p class="validateTips">All form fields are required.</p>
<form>
<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">
<!-- Allow form submission with keyboard without duplicating the dialog button -->
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form>
</div>
<div id="users-contain" class="ui-widget">
<h1>Existing Users:</h1>
<table id="users" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header ">
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>john.doe@example.com</td>
<td>johndoe1</td>
</tr>
</tbody>
</table>
</div>
<button id="create-user">Create new user</button>
</body>
</html>
请确保您拥有jquery ui和与该ui配套的css。您的代码很好,只需将其添加到<head>
:中即可
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
请参阅工作演示:http://jsfiddle.net/swm53ran/221/
看起来您导入了jquery而不是jquery ui。试着导入jqueryui,你应该会没事的。
在你的头上加上这行:
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
也添加这个css:
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
相关文章:
- 动态Javascript表单不起作用
- 带有select的jquery提交表单不起作用
- 下拉 jquery 方法发布表单不起作用
- JavaScript onChange 事件侦听器对输入表单不起作用
- 带有javascript按钮的PHP表单-不起作用
- 使用AJAX提交表单不起作用
- Jquery Modal/Dialog表单不起作用
- 使用Javascript提交表单不起作用
- 事件委派后表单不起作用
- 为什么在IE和Chrome中提交表单不起作用
- 根据<选择>动态创建的表单不起作用
- 在Kentico中使用response.write重定向HTML表单不起作用
- 移动响应表单不起作用
- 联系表单不起作用(Php 端)
- 使用 jQuery POST 和 php 序列化和提交表单不起作用
- 通过 ajax 提交表单不起作用
- 在 Javascript 中验证表单不起作用
- e.防止默认和 asp.net 登录表单不起作用
- 为什么我的拖放区 JavaScript 表单不起作用
- 使用 Javascript 提交表单不起作用