我有一个使用弹出窗口的jquery示例:
<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>
当我点击编辑时,它应该获取信息并编辑行但不起作用。
以下是demo
请有人帮助我吗?
答案 0 :(得分:1)
<强> DEMO 强>
这是代码js:
$(function() {
var dialog, form,
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,/^[a-z]([0-9a-z_\s])+$/i, "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" );
}
});
//create user dialog
/*dialogs = $( "#dialog-forms" ).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" );
});
//edit user dailog
edit_dialogs = $( "#edit-dialog-form" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Edit user details": editUser,
Cancel: function() {
edit_dialogs.dialog( "close" );
}
},
close: function() {
form[ 0 ].reset();
allFields.removeClass( "ui-state-error" );
}
});
edit_form = edit_dialogs.find( "form" ).on( "submit", function( event ) {
event.preventDefault();
editUser();
});
var current_edit_row_details={};
function editUser() {
var valid = true;
//console.log('current_edit_row_details = ');
//console.log(current_edit_row_details);
var tds = $(current_edit_row_details.current_row).children('td');
var name = $('#edit_name').val();//current_edit_row_details.name;
var email = $('#edit_email').val();//current_edit_row_details.email;
var password = $('#edit_password').val(); //current_edit_row_details.password;
tds[0].innerHTML = name;
tds[1].innerHTML = email;
tds[2].innerHTML = password;
edit_dialogs.dialog( "close" );
//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,/^[a-z]([0-9a-z_\s])+$/i, "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>" );
}*/
return valid;
}
$( ".edit-user" ).click(function() {
var tds = $(this).closest('tr').children('td');
//alert(tds[0].innerHTML)
var name = tds[0].innerHTML;
var email = tds[1].innerHTML;
var password = tds[2].innerHTML;
$('#edit_name').val(name);
$('#edit_email').val(email);
$('#edit_password').val(password);
current_edit_row_details.name = name;
current_edit_row_details.email = email;
current_edit_row_details.password = password;
current_edit_row_details.current_row = $(this).closest('tr');
edit_dialogs.dialog( "open" );
//console.log(tds);
});
});