禁用提交按钮后,我不工作preSS一个单引号我不、按钮、单引号、工作

2023-09-11 01:29:20 作者:我命由我不由天

图:

结果:

我有一个提交按钮(回复按钮)的问题没有被禁用后,我preSS'(单引号)或很多人,我只是希望它禁用。问题的关键是preventing有人键入单/双引号或一个奇怪的字符在文本区域。我从别人code。在这里,它工作在测试在不同的文件中,但这里的情况是不同的,形式是一个Ajax里面,我很newbe jQuery的,我不知道在哪里把该code键使它的工作原理。

系统小技巧 禁用无用服务 提高Windows 10性能

这是阿贾克斯的我简单的code:

 变种X;
功能clickreply(OBJ){
    VAR VARID = obj.id;
    VAR getnumb = varid.match(/ \ D / G); //从字符串获得数
    VAR idreply ='idreply'+ getnumb;
    执行console.log(VARID);
    执行console.log(getnumb);
    执行console.log(idreply);
    $阿贾克斯({
        网址:/logincheckmember2.php',//这是当前文档
        键入:POST,
        数据类型:'json的',//添加JSON数据类型来获得JSON
        成功 :
        功能(结果){
            X =结果['ssloginmember'];
            执行console.log(X);
            如果(结果['ssloginmember'] == NULL){
                msgBLshow();
            }其他{
                X =<中心>中+
                    <形式方法='后'的名字=myForm的'ID =myForm的'>中+
                        < textarea的ID ='tareply行=4COLS = '50'最大长度='250'占位符='最大长度= 250'>< / textarea的>< BR>中+
                        <输入类型=提交ID =submitreply'值='回复'的onclick ='clicktareply()'>< /输入>+
                    < /形式GT;+
                < /中心>中;
                的document.getElementById(msgcontent1)的innerHTML = X。
                $(#MSG1)fadeTo(1000,1);
            }
        }
    });
}
功能clicktareply(){
    警报(的document.getElementById(tareply)值);
}

$(文件)。就绪(函数(){
    $('#tareply)。KEYUP(函数(){
        如果(!?!$(本).VAL()匹配(/ ^(\多个)([α - 杂 -  Z0-9 _)及。]){1,} $ /克)){
           $('#submitreply')丙(禁用,真正的);
        }其他{
           $('#submitreply')丙(禁用,假);
        }
    });
});
 

解决方案

您必须使用键preSS ,而不是 KEYUP 。因为要prevent的singlequotes

  $(文件)。就绪(函数(){
    $(身体)。代表('#tareply','的keyup',函数(){
     //匹配条件..do你的逻辑在这里..personally我使用的indexOf而不是正则表达式
        如果(!?!$(本).VAL()匹配(/ ^(\多个)([α - 杂 -  Z0-9 _)及。]){1,} $ /克)){
           $('#submitreply')丙(禁用,真正的);
        }其他{
           $('#submitreply')丙(禁用,假);
        }
    });
});
 

image:

result:

I have a problem that the submit button (reply button) is not disabled after i press '(single quote) or many of them, I just want it disabled. The point is preventing someone type a single/double quote or a weird character on the textarea. I have a code from someone in here and it works in testing in a different file, but here the condition is different, the form is inside an ajax and I'm very newbe in Jquery, and i don't know where to put that code to make it works.

this is my simple code of ajax:

var x;
function clickreply(obj){
    var varid = obj.id;
    var getnumb = varid.match(/\d/g); //get number from string
    var idreply = 'idreply'+getnumb;
    console.log(varid);
    console.log(getnumb);
    console.log(idreply);
    $.ajax({
        url: '/logincheckmember2.php', //This is the current doc
        type: "POST",
        dataType:'json', // add json datatype to get json
        success : 
        function (result) {
            x=result['ssloginmember'];
            console.log(x);
            if(result['ssloginmember']==null){
                msgBLshow();
            }else{
                x="<center>"+
                    "<form method='post' name='myForm' id='myForm'>"+
                        "<textarea id='tareply' rows='4' cols='50' maxlength='250' placeholder='maxlength=250'></textarea><br>"+
                        "<input type='submit' id='submitreply' value='reply' onclick='clicktareply()'></input>"+
                    "</form>"+
                "</center>";
                document.getElementById("msgcontent1").innerHTML=x;
                $("#msg1").fadeTo(1000, 1);
            }                   
        }
    });
}
function clicktareply(){
    alert(document.getElementById("tareply").value);
}

$(document).ready(function() {
    $('#tareply').keyup(function() {
        if(!$(this).val().match(/^(?!\s)([a-zA-Z0-9 _.)?&]){1,}$/g)) {
           $('#submitreply').prop('disabled', true);
        }else{
           $('#submitreply').prop('disabled', false);
        }
    });
});

解决方案

you have to use keypress instead of keyup .because you want to prevent the singlequotes

$(document).ready(function() {
    $('body').delegate('#tareply','keyup',function() {
     //match condition ..do your logic here ..personally i use indexOf rather than regexp
        if(!$(this).val().match(/^(?!\s)([a-zA-Z0-9 _.)?&]){1,}$/g)) {
           $('#submitreply').prop('disabled', true);
        }else{
           $('#submitreply').prop('disabled', false);
        }
    });
});