[英]jQuery form submitting isn't submitting the form
I got a basic HTML form which I validate through jQuery, 我有一个基本的HTML表单,可以通过jQuery进行验证,
this is the form: 形式如下:
<form method="post" action="index.php" enctype="multipart/form-data" id="add_product">
מק"ט:
<input type="text" name = "product_cn" value="<?php echo $product_cn;?>" id="cn"/> <span style="margin-right: 20px; color:red; display:none" id="cn_exist"></span>
<br /> <br />
שם המוצר:
<input type="text" name="product_name" value="<?php echo $product_name;?>" id="p_name"/>
<br /> <br />
פרטי המוצר:
<textarea rows = "6" cols = "30" name="product_details" id="p_details"> <?php echo $product_details;?></textarea>
<br /> <br />
מחיר:
<input type="text" name = "product_price" value="<?php echo $product_price;?>" id="p_price"/>
<br /> <br />
תמונה:
<input type="file" name="fileField" id="p_image" />
<br /> <br />
<input type="submit" name="submit" value="רשום מוצר" />
</form>
this is my validation code: 这是我的验证代码:
$("form").submit(function(e){
e.preventDefault(e);
var p_cn = $("#cn").val();
var p_name = $("#p_name").val();
var p_details = $("#p_details").val();
var p_price = $("#p_price").val();
var p_image = $("#p_image").val();
error = "";
if(!(p_cn != "") || !(p_cn.length > 0)){
error += " אנא הוסף הוסף מק\"ט <br /> <br />" ;
}
if(!(p_name != "") || !(p_name.length > 0)){
error += "אנא הזן שם מוצר <br /> <br />";
}
if(!(p_price != "") || !(p_name.length > 0)){
error += " אנא הזמן מחיר. <br /> <br />";
}
if(error != ""){
$("#form_errors").html(error).hide().slideDown(500);
}else{
$("#form_errors").slideUp(500);
$("#add_product").submit();
}
});
Please ignore any character you don't understand, it's my language. 请忽略您不理解的任何字符,这是我的语言。
As you can see, what I did is prevent the form from being submitted, I had to use the selector "form" or otherwise, it just won't work for some reason >_<. 如您所见,我所做的是阻止提交表单,我不得不使用选择器“表单”,否则,出于某些原因> _ <就无法使用。 Therefore, I tried using the selector "form" and it worked.
因此,我尝试使用选择器“窗体”,它起作用了。
Now, as you can see in the last IF condition, I want to submit the form if the variable "error" is empty, but the form just doesnt submit. 现在,如您在最后一个IF条件中所看到的,如果变量“ error”为空,我想提交表单,但是表单不提交。 I would like to know how to make the form submit :-)
我想知道如何使表格提交:-)
Thanks in advance! 提前致谢!
e.preventDefault(e);
(which should be just e.preventDefault();
) stops the submit from happening. (应该只是
e.preventDefault();
)阻止提交的发生。 You need to do that conditionally: 您需要有条件地这样做:
if (error) {
e.preventDefault();
} else {
// No need to prevent the submit
}
The purpose of e.preventDefault()
is to stop the element's default action. e.preventDefault()
的用途是停止元素的默认操作。 In a form.submit(), it prevents the form from being submitted. 在form.submit()中,它防止提交表单。
Here is a detailed explanation 这是详细的说明
Move the preventDefault() to here: 将preventDefault()移到此处:
if(error != ""){
$("#form_errors").html(error).hide().slideDown(500);
e.preventDefault(); //do NOT want it to submit if there are errors
}else{
$("#form_errors").slideUp(500);
$("#add_product").submit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.