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 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?