通过鼠标单击提交表单并输入按钮

时间:2012-04-13 17:02:15

标签: php jquery ajax

我通过MOUSE CLICK和ENTER提交表单。

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

JQUERY

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将提交,我不想重新加载页面。当我按下

时,AJAX无法正常工作

1 个答案:

答案 0 :(得分: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字符串。