我正在尝试验证一个简单的表单,但不知何故它没有发生。我需要在客户端验证表单(服务器端 - 我能够做,但效率不高),然后将其传递到php文件以进行必要的操作。
我到目前为止所尝试的代码是:
abc.js
$("#makeproj-nav").click(function()
{
s = "<div class='create_project_card'><div class='new_project_title'><span>New Project</span></div>";
// project form starts
s += "<form id = 'create_new_form' name = 'new_form' method='post' action='insert_new_project.php'><div class='project_form'><span>";
s += "<div class='Form_contents'> <div> Project Name: <input type='text' size='100' name='project_name'></div>";
s += "<div class='Form_contents'> <input type='submit'> </div>";
s += "<span></div>";
$("#card-wrapper").html(s);
$('#create_new_form').on('submit', function(e) {
e.preventDefault();
var x = document.forms["new_form"]["project_name"].value;
if (x==null || x="")
{alert("Not Valid"); return false;}
else
{
$.ajax({
url : 'insert_new_project.php',
type : 'POST',
data : $(this).serialize(),
success : function(response) {
$("#card-wrapper").html(response.reply);
}
});
}
});
});
我错过了什么吗?