为什么一种方式有效而另一种方式无效?



我有以下代码:

if(chk) {
document.getElementById(messagebox).innerHTML="update do be done";
document.getElementById(messagebox).style.color="green"; 
} else {
document.getElementById(messagebox).innerHTML="no update";
document.getElementById(messagebox).style.color="red"; 
};  
});

chk是一个来自这里的变量:

var chk = ((lenta >= 1) && (lenta <= 5) && (str.search(/[0-9¬!"£$%^&*()_-+={}[]#~@';:/?>.<,\ |]/) <= -1));

基本上,如果chk为true,则表示输入是正确的。在这种情况下,输入只允许字母和长度在1到5之间。

脚本运行良好,但如果不是";

document.getElementById(messagebox).innerHTML="update do be done";
document.getElementById(messagebox).style.color="green";

"我补充">

$.ajax({
type: 'POST',  
data: ({quoteprefix : str}),
url: 'liveupdate/firstsetup.php',
success: function(data) {
document.getElementById(messagebox).innerHTML=" <?php echo pw('updatedone'); ?>. "+data+"";
document.getElementById(messagebox).style.color="green";
} 
});
"

无论输入值如何,脚本都会更新示例:如果我输入AB,它会更新AB。如果我输入AB123;AB";

完整的脚本在这里:

$(document).ready(function() {
var ffind = "#quoteprefix"; //input value id
$(ffind).on("change paste keyup", function() {
var messagebox = "qquoteprefix"; //message box
var lenta = $(this).val().length;
var str = $(this).val();
var chk = ((lenta >= 1) && (lenta <= 5) && (str.search(/[0-9¬!"£$%^&*()_-+={}[]#~@';:/?>.<,\ |]/) <= -1));

if (chk) {
$(this).removeClass("is-invalid");
$(this).addClass("is-valid");
} else {
$(this).removeClass("is-valid");
$(this).addClass("is-invalid");
};


}); 
$(ffind).on("focusout", function() {

if(chk) {
document.getElementById(messagebox).innerHTML="update de facut";
document.getElementById(messagebox).style.color="green"; 
} else {
document.getElementById(messagebox).innerHTML="nu face update" +chk;
document.getElementById(messagebox).style.color="red"; 
};  
});


});

更新:尝试过这个,结果相同:

$(document).ready(function() {
var ffind = "#quoteprefix"; 
$(ffind).on("change paste keyup", function() {
var messagebox = "qquoteprefix";
var lenta = $(this).val().length;
var str = $(this).val();
var chk=false;
var chk = ((lenta >= 1) && (lenta <= 5) && (str.search(/[0-9¬!"£$%^&*()_-+={}[]#~@';:/?>.<,\ |]/) <= -1));

if (chk) {
$(this).removeClass("is-invalid");
$(this).addClass("is-valid");
$(this).focusout( function() {
$.ajax({
type: 'POST', 
data: ({quoteprefix : $(this).val()}),
url: 'liveupdate/firstsetup.php',
success: function(data) {
document.getElementById(messagebox).innerHTML=" <?php echo pw('updatedone'); ?>."+chk;
document.getElementById(messagebox).style.color="green";
} 
});
});
} else {
$(this).removeClass("is-valid");
$(this).addClass("is-invalid");
};

}); 

});

为什么?有人能帮我吗

所以,我发现这很有效:

$(document).ready(function() {
var ffind = "#<?php echo $field; ?>"; 
var messagebox = "<?php echo $field[0].$field; ?>";
$(ffind).on("change paste keyup", function() {
var notallowedchars = /[0-9¬!"£$%^&*()_-+={}[]#~@';:/?>.<,\ |]/;

var chk = (($(this).val().length >= 1) && 
($(this).val().length <= 5) && 
($(this).val().search(notallowedchars) <= -1));

if (chk) {
$(this).removeClass("is-invalid");
$(this).addClass("is-valid");
} else {
$(this).removeClass("is-valid");
$(this).addClass("is-invalid");
};

$(this).focusout( function() {
var chk = (($(this).val().length >= 1) && 
($(this).val().length <= 5) && 
($(this).val().search(notallowedchars) <= -1));
if (chk) {
$.ajax({
type: 'POST', 
data: ({quoteprefix : $(this).val()}),
url: 'liveupdate/firstsetup.php',
success: function(data) {
document.getElementById(messagebox).innerHTML=" <?php echo pw('updatedone'); ?>.";
document.getElementById(messagebox).style.color="green";
} 
});
} else {
document.getElementById(messagebox).innerHTML="<?php echo pw('noupdatedone'); ?>";
document.getElementById(messagebox).style.color="red";
}
}); 
}); 
});

显然在";focusout";函数我需要再次进行所有检查。

最新更新