1

我也通过鼠标单击和输入提交表单。

Ajax Call 正在检查我已经在数据库中是否有任何指定。如果没有,用户可以提交表单,否则提交按钮将禁用

查询

function check_designation(e){

    text = $('#req1').val();
    data = "data=" + text;          
    text_length = text.length           

    if(text_length == 0)
    {
        $('#result_span').html('');
    }       

    if(text_length > 3 ){
        $.ajax({
            url: "designation_ajax.php",
            type: "POST",
            data: data,    
            cache: false,
            success: function (response) {
                if ($.trim(response) == "access") { 
                    $("#result_span").html('<div class="green">' + text + ' is available '+'</div>');
                    $('#create_desg').removeAttr('disabled');
                }
                else if ($.trim(response) == "no access") {                                                         
                    $("#result_span").html('<div class="red">' + text + ' is already in use'+'</div>'); 
                    $('#create_desg').attr('disabled','disabled');                                                      
                }

                else { 

                    alert('Sorry, unexpected error. Please try again later.');

                }
            }      
        }); 
    }
    else{
        $("#result_span").html('');     

    }

    return true;        
}

HTML 表格

<form id="formID" class="formular" method="POST" action="" onsubmit="formSubmit()" >
  <fieldset>
    <legend>Create Desination</legend>
    <label> Designation<br clear="all" />
      <input autocomplete="off" onkeyup="check_designation(event)" value="" class="validate[required,minSize[4]] text-input float_left" type="text" name="name" id="req1" />
      <span id="result_span"></span>
    </label>
    <br clear="all" />
    <input id="create_desg" value="Submit" type="button" />
  </fieldset>
</form>

问题::::

现在会发生什么 DISABLE 按钮不是解决方案...如果表中已经有一个 DESIGNATION .. 提交按钮将禁用,但按 ENTER 它将提交,我不想重新加载页面。当我按下 ENTER 时,AJAX 不工作

4

1 回答 1

4

您必须从onsubmit处理程序返回 false 才能取消默认操作。但我可能会稍微清理一下您的代码并悄悄订阅提交事件:

<form id="formID" class="formular" method="POST" action="">
    <fieldset>
        <legend>Create Desination</legend>
        <label>
            Designation<br clear="all" />
            <input autocomplete="off" value="" class="validate[required,minSize[4]] text-input float_left" type="text" name="name" id="req1" />
            <span id="result_span"></span>
        </label>
        <br clear="all" />
        <input id="create_desg" value="Submit" type="button" />
    </fieldset>
</form>

您会注意到我故意onkeyup从输入字段中删除了该事件。每次某些用户在此字段内按下某个键时,用 AJAX 请求敲击您的服务器对您的服务器没有任何好处。如果您想实现这一点,我建议您在发送 AJAX 请求之前等待一些输入累积和限制。

进而:

$(function() {
    $('#formID').submit(function() {
        var text = $('#req1').val();
        if(text.length == 0) {
            $('#result_span').html('');
        }       

        if(text.length > 3) {
            $.ajax({
                url: 'designation_ajax.php',
                type: 'POST',
                data: { data: text },
                cache: false,
                success: function (response) {
                    if ($.trim(response) == 'access') { 
                        $('#result_span').html('<div class="green">' + text + ' is available '+'</div>');
                        $('#create_desg').removeAttr('disabled');
                    }
                    else if ($.trim(response) == 'no access') {
                        $("#result_span").html('<div class="red">' + text + ' is already in use'+'</div>'); 
                        $('#create_desg').attr('disabled', 'disabled');
                    } else { 
                        alert('Sorry, unexpected error. Please try again later.');
                    }
                }      
            }); 
        } else {
            $('#result_span').html('');     
        }

        // return false to prevent the default action
        return false;
    });
});

此外,我会让designation_ajax.php脚本返回 JSON,而不是您在回调中解析access和修剪的一些字符串。no accesssuccess

于 2012-04-13T17:04:50.147 回答