dongyuli0964 2012-04-13 17:02
浏览 67
已采纳

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

I am Submiting form through MOUSE CLICK and ENTER too.

Ajax Call is checking is there any designation which i already in DATABASE.. If not, user can submit form otherwise SUBMIT button will DISABLE

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

<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>

PROBLEM::::

Now what happen DISABLE button is not a solution... if there is already a DESIGNATION in a table.. submit button will disable but By ENTER it will submitted and i dont want to reload the page. and AJAX is not working when i PRESS ENTER

  • 写回答

1条回答 默认 最新

  • duan1227 2012-04-13 17:04
    关注

    You must return false from your onsubmit handler in order to cancel the default action. But I would probably clean your code a bit and subscribe to the submit event unobtrusively:

    <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>
    

    You will notice that I have intentionally removed the onkeyup event from the input field. Hammering your server with AJAX requests every time some user hits a key while inside this field won't do any good to your server. If you want to implement this I would recommend you waiting for some input to accumulate and throttle before sending the AJAX request.

    and then:

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

    Also I would have the designation_ajax.php script return JSON instead of some access and no access strings that you are parsing and trimming in your success callback.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应