2016-11-28 27 views
2

當我點擊提交按鈕,我不能接受的價值yearend。相反,我得到了一個未定義的值。點擊提交按鈕後,如何獲得yearend值?如何使用AJAX數據變量使用的情況下提交按鈕

我的代碼:

$("#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); 
+0

你能否解釋一下?你在註釋掉的語句是執行它還是執行這個確切的代碼? –

回答

2
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

+0

Thanxz它的工作現在 – faizan

+0

是您給予的答案插件ryte..thanxz – faizan

1

我想這是因爲VAR的範圍。您必須在請求前定義變量,然後重新定義它並在AJAX請求之後獲取它。

0

這就是你的Ajax調用yearned外使用Ajax預期的行爲是undefined。把你的點擊功能放在ajax調用中。所以,一旦你的ajax調用完成併成功了,你將會在成功函數中附加你的事件監聽器。使用這個邏輯來代替:

$.ajax({ 
           type: "GET", 
           url: callpage, 
           async: false, 
           success: function(data) { 
           yearend= data, 
           $("#content").html("") 
           $("#content").html(data) 
           $('#submit').live('click',function(){ 
           //var yearend = "<?php echo $_SESSION['yearend'] ; ?>" 

           alert("this is submit button"+yearend); 
           }); 

           } 


          }); 
1
<!DOCTYPE html> 
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $.ajax({ 
           type: "GET", 
           url: callpage, 
           async: false, 
           success: function(data) { 
           yearend= data, 
           $("#content").html("") 
           $("#content").html(data) 
           $('#submit').live('click',function(){ 

           alert("this is submit button"+yearend); 
           }); 

           } 

          }); 
}); 
</script> 
</head> 
<body> 

</body> 
</html> 
+0

僅供參考,.live()方法已過時 –

相關問題