【问题标题】:Is there a way to prevent Enter key from returning a response in json from another page? Codeigniter Ajax有没有办法防止 Enter 键从另一个页面返回 json 响应?代码点火器阿贾克斯
【发布时间】:2020-03-16 04:44:16
【问题描述】:

我正在使用 ajax 进行操作 CreateUpdate

我已经可以创建和更新了,但是如果我按或按Enter键然后从另一个页面显示,这有点不好看,并且在点击Enter 键。

{"success":false,"type":"update"}

对于视觉表示,这里是在输入字段上按Enter键后的样子

这就是我在视图中输入的内容

 <input type="text" name="group[]" id="group" placeholder="Enter your Choice" class="form-control" />

在我的控制器中

public function addGroup(){
    $result = $this->group_model->addGroup();
    $msg['success'] = false;
    $msg['type'] = 'add';
    if($result){
        $msg['success'] = true;
    }
    echo json_encode($msg);
}


public function updateGroup(){
    $result = $this->group_model->updateGroup();
    $msg['success'] = false;
    $msg['type'] = 'update';
    if($result){
        $msg['success'] = true;
    }
    echo json_encode($msg);
}

在模型中

public function updateGroup(){
    $id = $this->input->post('txtId');
    $field = array(
    'group_name'=>$this->input->post('group')
    );
    $this->db->where('id', $id);
    $this->db->update('groups', $field);
    if($this->db->affected_rows() > 0){
        return true;
    }else{
        return false;
    }
}



public function addGroup(){
    $field = array(
        'group_name'=>$this->input->post('group'),
    );
    $this->db->insert('groups', $field);
    if($this->db->affected_rows() > 0){
        return true;
    }else{
        return false;
    }
}

阿贾克斯

$('#btnSave').click(function(){
        var url = $('#myForm').attr('action');
        var data = $('#myForm').serialize();
        //validate form
        var group = document.getElementById('group').value;

        if(group.replace(/\s/g, "").length <=0 ) {
            swal("Submission fail!", "Enter the required field", "error");
            return false;
        }

            $.ajax({
                type: 'ajax',
                method: 'post',
                url: url,
                data: data,
                async: false,
                dataType: 'json',
                success: function(response){
                    if(response.success){
                        $('#myModal').modal('hide');
                        $('#myForm')[0].reset();
                        if(response.type=='add'){
                            var type = 'added'
                        }else if(response.type=='update'){
                            var type = 'updated'
                        }

                        swal("Success!", "You delete a Question!", "success");                          
                        showGroups();
                    }else{
                        alert('Error');
                    }
                },
                error: function(){
                    alert('Could not add data');
                }
            });

    });

【问题讨论】:

  • 你的 ajax 代码在哪里也显示了 ..
  • @BoominathanElango - 更新了我上面的代码,你可以在底部看到它。谢谢!
  • 问题是你可以禁用回车键直到你得到正确的响应?

标签: javascript php json ajax codeigniter


【解决方案1】:
$('#myForm').submit(function(e){
    $('#btnSave').attr('disabled',true); //disable the button

    //.Your other code

    success: function(response){
            if(response.success){
                $('#btnSave').attr('disabled',false); //enable the button

            }
    }
}

在单击按钮时,如果您按 Entern,则禁用 submnit 按钮的单击事件,这是默认行为,如果您获得成功响应,则启用它。

另外,如果你只是想禁用回车键,那么你可以这样做:

var keyCode = e.keyCode || e.which;
  if (keyCode === 13) { 
    e.preventDefault();
    return false;
  }

【讨论】:

  • 方法 1 不起作用,单击文本框后,alert('Error') 显示
  • 所以您想在输入文本框后立即禁用@MadaraCode Enter 键?
  • 你好!方法 2 有效,我添加了此代码 $('#choice_fill').keypress(function(e) { var keyCode = e.keyCode || e.which; if (keyCode === 13) { e.preventDefault(); return false; } }); 我如何添加添加 ID?所以我不会输入多个keypress 函数
  • @MadaraCode 只需给出类而不是 id 就可以了,如果它解决了您的问题,您可以接受答案
【解决方案2】:

改变

$('#btnSave').click(function(){
    var url = $('#myForm').attr('action');
    var data = $('#myForm').serialize();
    //validate form
    var group = document.getElementById('group').value;

    if(group.replace(/\s/g, "").length <=0 ) {
        swal("Submission fail!", "Enter the required field", "error");
        return false;
    }

        $.ajax({
            type: 'ajax',
            method: 'post',
            url: url,
            data: data,
            async: false,
            dataType: 'json',
            success: function(response){
                if(response.success){
                    $('#myModal').modal('hide');
                    $('#myForm')[0].reset();
                    if(response.type=='add'){
                        var type = 'added'
                    }else if(response.type=='update'){
                        var type = 'updated'
                    }

                    swal("Success!", "You delete a Question!", "success");                          
                    showGroups();
                }else{
                    alert('Error');
                }
            },
            error: function(){
                alert('Could not add data');
            }
        });

});

进入

$('#myForm').submit(function(e){
e.preventDefault();
    var url = $('#myForm').attr('action');
    var data = $('#myForm').serialize();
    //validate form
    var group = document.getElementById('group').value;

    if(group.replace(/\s/g, "").length <=0 ) {
        swal("Submission fail!", "Enter the required field", "error");
        return false;
    }

        $.ajax({
            type: 'ajax',
            method: 'post',
            url: url,
            data: data,
            async: false,
            dataType: 'json',
            beforeSend: function(){
            $('#btnSave').attr('disabled',true);
            },
            success: function(response){
                if(response.success){
                    $('#myModal').modal('hide');
                    $('#myForm')[0].reset();
                    if(response.type=='add'){
                        var type = 'added'
                    }else if(response.type=='update'){
                        var type = 'updated'
                    }

                    swal("Success!", "You delete a Question!", "success");                          
                    showGroups();
                }else{
                    alert('Error');
                }

            },
            error: function(){
                alert('Could not add data');
            }
            $('#btnSave').attr('disabled',false);

返回假; });

});

按钮改为提交

希望这会有所帮助!

【讨论】:

  • 显示的数据在哪里消失,点击alert('Error')后出现。
  • 检查在浏览器中按F12或右键单击浏览器>检查元素>网络检查结果那里到底发生了什么
猜你喜欢
  • 1970-01-01
  • 2012-11-23
  • 2011-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 2010-10-11
相关资源
最近更新 更多