javascript - 我如何在提交按钮下使用ajax数据变量

标签 javascript jquery ajax

当我点击提交按钮时,我无法收到值yearend。相反,我得到了一个未定义的值。单击提交按钮时如何获取yearend值?

我的代码:

$("#company").change(function() {

    $("#dFrom").val("");
    $("#dTo").val("");
    var pass_code = $("#company").val();
    var callpage = "dentrycomp.php?pass_code=" + pass_code
    var yearend= null;
    $.ajax({
        type: "GET",
        url: callpage,
        async: false,
        success: function(data) {
            yearend= data,
            $("#content").html("")
            $("#content").html(data)
        }
    });
    //var yearend = "<?php echo $_SESSION['yearend'] ; ?>"
    alert(yearend +"company");
    this alert box getting the right value yearend.i want that value recieve in under submit button                         
    return true;
});


$('#submit').live('click',function() {
//var yearend = "<?php echo $_SESSION['yearend'] ; ?>"
alert("this is submit button"+yearend);

最佳答案

var yearend = null;

$("#company").change(function() {

    $("#dFrom").val("");
    $("#dTo").val("");
    var pass_code = $("#company").val();
    var callpage = "dentrycomp.php?pass_code=" + pass_code

    $.ajax({
        type: "GET",
        url: callpage,
        async: false,
        success: function(data) {
            yearend = data,
            $("#content").html("")
            $("#content").html(data)
        }
    });

    //var yearend = "<?php echo $_SESSION['yearend'] ; ?>"
    alert(yearend + "company");
    this alert box getting the right value yearend.i want that value recieve in under submit button
    return true;
});

$('#submit').live('click', function() {
//var yearend = "<?php echo $_SESSION['yearend'] ; ?>"
alert("this is submit button" + yearend); });

您应该全局声明yearend,即在代码顶部。

关于javascript - 我如何在提交按钮下使用ajax数据变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40837919/

相关文章:

javascript - 我如何通过ajax解析html

使用 reduce 将 Javascript 对象转换为查询字符串

javascript - 无法从本地主机文件获取 json 数据 - ajax、jquery

javascript - 使拖放功能遍及所有网站,而不仅仅是特定的 div 部分

javascript - 在 ASP.Net MVC 中使用 ajax 通过 Id 检查单选按钮

javascript - 使用 AJAX 调用 php 函数一直失败

javascript - 如何按对象的值对对象数组进行排序?

javascript - 向右显示滚动条?

javascript - 视频框打开时防止滚动

jquery - Electron 应用的 Angular-CLI