[英]JQuery Ajax send data from form not working
My script should prevent the page to be refreshed or redirected, but when i click submit button it redirects me. 我的脚本应阻止页面刷新或重定向,但是当我单击“提交”按钮时,它将重定向我。
Error Message from JavaScript console 来自JavaScript控制台的错误消息
Uncaught TypeError: $(...).val(...).len is not a function
未捕获的TypeError:$(...)。val(...)。len不是函数
myScript.js myScript.js
$( document ).ready(function() {
console.log( "Ready!" );
//Submitting the form data
$("#user-form").submit(function(e){
if($('#fName').val().len() != "" && $('#lName').val().len() != "" && $('#nickname').val().len() != "") {
$.ajax({
type: "POST",
url: "user-process.php",
data: $("#user-form").serialize(),
success: function(data){
$('#user-form')[0].reset();
console.log(data); // show response from the php script.
}
});
}
else {
alert("Please fill the fields.")
}
e.preventDefault();
});
setInterval(function(){ $(`#table`).load('info.php #table'); }, 1000); //Loading table data without refreshing page.
});
尝试使用.length而不是.len() https://api.jquery.com/length/
val()
returns a string. val()
返回一个字符串。 Strings don't have .len()
function. 字符串没有
.len()
函数。 Also, since you're comparing it with another string, you don't need to get length, just use this: 另外,由于您正在将其与另一个字符串进行比较,因此不需要获取长度,只需使用以下命令即可:
if($('#fName').val() != "" && $('#lName').val() != "" && $('#nickname').val() != "") {
If you want to check length, you can get it using .length
. 如果要检查长度,可以使用
.length
来获取。 But then you should compare it with 0
, not with ""
: 但是,您应该将其与
0
而不是与""
:
if($('#fName').val().length != 0 && $('#lName').val().length != 0 && $('#nickname').val().length != 0) {
Things like $('#fName').val().length != ""
would technically work, because JavaScript will convert ""
into 0
for you, but it's a bad coding practice, as it makes the code hard to follow. 像
$('#fName').val().length != ""
在技术上是可行的,因为JavaScript会为您将""
转换为0
,但这是一种不好的编码习惯,因为它使代码难以遵循。
To know if a string is empty or not, you can use the following statements : 要知道一个字符串是否为空,可以使用以下语句:
$(selector).val() !== ''
!$(selector).val().length
$(selector).val().length === 0
(Since a string length will never be negative, the two last ones are equal) (由于字符串长度永远不会为负,因此最后两个相等)
About the redirection fact, take a look at the stopPropagation and stopImmediatePropagation functions 关于重定向事实,请查看stopPropagation和stopImmediatePropagation函数
https://api.jquery.com/event.stopimmediatepropagation/ https://api.jquery.com/event.stoppropagation/ https://api.jquery.com/event.stopimmediatepropagation/ https://api.jquery.com/event.stoppropagation/
"len()" is not a javascript function, you can modify your code to be : “ len()”不是javascript函数,您可以将代码修改为:
if( $('#fName').val() && $('#lName').val() && $('#nickname').val() )
and will work 并且会工作
In JavaScript, String
object don't have len()
function, instead they have length
property. 在JavaScript中,
String
对象没有len()
函数,而是具有length
属性。
Usage: 用法:
var myString = "Hello";
console.log(myString.length); // returns 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.