我有一个现有表,我想防止将相同的数据添加到现有表中。
我的问题:为什么当我点击“创建新用户”按钮时,结果总是提示“未找到”?
这是代码: HTML:
<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>guruh</td>
<td>guruhkharisma1@yahoo.com</td>
<td>123456</td>
</tr>
<tr>
<td>edo</td>
<td>edo@yahoo.com</td>
<td>123456</td>
</tr>
</tbody>
</table>
</div>
<button id="create-user">Create new user</button>
jQuery
$(function() {
var name = $( "#name" ),
email = $( "#email" ),
password = $( "#password" ),
allFields = $( [] ).add( name ).add( email ).add( password ),
tips = $( ".validateTips" );
function checkExisting2(o) {
var arr = [];
$('#users-contain tr').each(function() {
if (!this.rowIndex) return;
var Id = $(this).find("td").eq(0).html();
arr.push(Id);
});
if ( $.inArray(o,arr) > -1) {
return false;
} else {
return true;
}
}
$( "#dialog-form" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Create an account": function() {
var bValid = true;
bValid = bValid && checkExisting2(name);
if ( bValid ) {
alert("not found");
$( "#users tbody" ).append( "<tr>" +
"<td>" + name.val() + "</td>" +
"<td>" + email.val() + "</td>" +
"<td>" + password.val() + "</td>" +
"</tr>" );
$( this ).dialog( "close" );
} else {
alert("found");
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
});
$( "#create-user" )
.button()
.click(function() {
$( "#dialog-form" ).dialog( "open" );
});
});