dsfjnxjlbqv9812 2016-02-22 11:17
浏览 24
已采纳

在onKeyup AJAX调用第一次不工作或后退

var d=0;
    function checkhrsforDate(){
        var date=$('#approveapplieddate').val();
        $.ajax({
            url: "datelogic.php",
            type: "post",
            data:'date='+date,
            cache: false,
            success:function(htmlData){
                if(htmlData=='')
                {
                    d=0;
                }
                else{
                    d=htmlData;
                }
                return d;
            }
        });
    }
    $(document).ready(function(){
        $("#approveappliedhrs").onkeyup(function (e) {
                var approvedate=$('#approvedate').val();
                var approveapplieddate=$('#approveapplieddate').val();
                appliedhrs=$("#appliedhrs").val();
                hrs=parseInt($(this).val());
                if($(this).val().length==1){
                    checkhrsforDate();
            }
        });
    });

First time it showing d value as 0 and next key press it's working fine. But my textbox allows only one value. is it possible to call AJAX call in keyup event

  • 写回答

1条回答 默认 最新

  • doumu9019 2016-02-22 11:31
    关注

    onkeyup is a javascript event,

    CORRECTION

    function checkhrsforDate(){
    var d = 0;
    var date = $('#approveapplieddate').val();
    $.ajax({
        url: "datelogic.php",
        type: "post",
        data:{date: date},
        cache: false,
        success:function(htmlData){
            if(htmlData=='')
            {
                d = 0;
            }
            else{
                d = htmlData;
            }
            return d;
        }
    });
    }
    $("#approveappliedhrs").keyup(function(e) {
    var approvedate = $('#approvedate').val();
    var approveapplieddate = $('#approveapplieddate').val();
    appliedhrs = $("#appliedhrs").val();
    hrs = parseInt($(this).val());
    if ($(this).val().length == 1) {
        checkhrsforDate();
    }
    });
    

    value of d will depend upon what you return. If it is json string then you can check like if(htmlData=='').

    For better answer paste your ajax response

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

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?