Jquery 用户名可用性有效,但表单在不可用时仍提交
我有检查用户名可用性的表单,其中只有用户名可用时表单才应提交数据,但在这两种情况下表单都提交数据这里是代码Jquery 用户名可用性有效,但表单在不可用时仍提交,jquery,Jquery,我有检查用户名可用性的表单,其中只有用户名可用时表单才应提交数据,但在这两种情况下表单都提交数据这里是代码 <script type="text/javascript"> $(document).ready(function () //When the dom is ready { $("#username").change(function () { //if theres a change in the username textbox var username =
<script type="text/javascript">
$(document).ready(function () //When the dom is ready
{
$("#username").change(function () { //if theres a change in the username textbox
var username = $("#username").val(); //Get the value in the username textbox
if (username.length > 3) //if the lenght greater than 3 characters
{
$("#availability_status").html('<img src="loader.gif" align="absmiddle"> Checking availability...');
//Add a loading image in the span id="availability_status"
$.ajax({ //Make the Ajax Request
type: "POST",
url: "http://localhost/Testtt/wp-content/plugins/Realestate Redirection/check_realitycode_availablity.php",
//file name
data: "username=" + username,
//data
success: function (server_response) {
$("#availability_status").ajaxComplete(function (event, request) {
if (server_response == '0') //if ajax_check_username.php return value "0"
{
$("#availability_status").html('<img src="available.png" align="absmiddle"> <font color="Green"> Available </font> ');
//add this image to the span with id "availability_status"
} else if (server_response == '1') //if it returns "1"
{
$("#availability_status").html('<img src="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
}
});
}
});
} else {
$("#availability_status").html('<font color="#cc0000">Username too short</font>');
//if in case the username is less than or equal 3 characters only
}
return false;
});
});
</script>
这是检查可用性的JQuery
这是我的表格
<form action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" name="reality" method="post" id="reality_form" >
<input type="hidden" name="reality_hidden" value="Y">
Website Url:<input type="text" name="website_url" value="" />
Listing Code: <input type="text" name="rlt_code" id="username" /><span id="availability_status"></span>
<input type="submit" name="submit" value="submit" id="submit" />
</form>
我试图找到过去几个小时的解决方案,但没有结果。请告诉我在ifserver_response=='1'的情况下需要添加哪些代码来阻止表单提交。在这种情况下,您可以先用类标记:
<form class='no-submit' action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" name="reality" method="post" id="reality_form" >
然后,为表单添加提交处理程序:
$('#reality_form').submit() {
if ($(this).hasClass('no-submit'))
return false;
});
您必须检查用户名是否在服务器上可用,因为您无法100%确定在提交表单时是否已完成AJAX检查。现在,您的代码中没有任何内容会阻止表单提交。表单的某个地方有提交处理程序吗?我应该在哪里添加提交处理程序?我只是问是否有提交处理程序。没有,我没有注意到我以前使用的代码在提交时执行此操作,但这对我不起作用,所以我遵循了此操作。但我是jQuery搜索新手,从过去3个小时开始一直没有得到正确的解决方案。你能发布一些解决方案吗。用户名不可用时不应提交表单
$('#reality_form').submit() {
if ($(this).hasClass('no-submit'))
return false;
});