Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么一种方法有效而另一种方法无效';T_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 为什么一种方法有效而另一种方法无效';T

Javascript 为什么一种方法有效而另一种方法无效';T,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有以下代码: if(chk) { document.getElementById(messagebox).innerHTML="update do be done"; document.getElementById(messagebox).style.color="green"; } else { document.getElementById(messagebox).inner

我有以下代码:

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));
”我补充道 "

$.ajax({
键入:“POST”,
数据:({quoteprefix:str}),
url:'liveupdate/firstsetup.php',
成功:功能(数据){
document.getElementById(messagebox.innerHTML=“.”+数据+”;
document.getElementById(messagebox).style.color=“绿色”;
} 
});
"
不管输入值是多少,脚本都会更新 示例:如果我输入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"; 
        };  
});




});
$(文档).ready(函数(){
var ffind=“#quoteprefix”//输入值id
$(ffind).on(“更改粘贴键控”,函数(){
var messagebox=“qquoteprefix”;//消息框
var lenta=$(this).val().length;
var str=$(this.val();

var chk=((lenta>=1)和&(lenta因此,我发现这是可行的:

$(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";
    }
}); 

}); 

});
$(文档).ready(函数(){
var ffind=“#”;
var messagebox=“”;
$(ffind).on(“更改粘贴键控”,函数(){
var notallowedchars=/[0-9,,!“\$%^\&\*(\)\\\-+\=\{\\\\[\]\\\~@\\\\:\/\?>\.=1)和

($(this).val().length您试图解决什么问题?您被困在哪里?尝试将focusout函数放入change、paste、keyup中..您真的打算使用
str.search(…)=
。另外,如果服务器不先执行PHP代码,就没有必要将其直接放入HTML中。wais,str.search(…)不能包含这些字符。
$(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";
    }
}); 

}); 

});