jQuery变量不会改变

jQuery var doesn't change

本文关键字:改变 变量 jQuery      更新时间:2023-09-26

我有一个AJAX注册表单:

var id = null;
$.ajax({
    type: 'POST',
    url: requestUrl,
    data: $(".defaultRequest").serialize(),
    dataType: 'json',
    success: function(data) {
        if(data.response){
            $('div.errormsg').remove();
            if(data.step){
                openStep(data.step);
            }else{
                    openStep('next');
            }
        }else{
            $('div.errormsg').remove();
            $('<div class="errormsg">'+data.message+"</div>").insertBefore(form);
        }
    }
});

当用户成功注册时,我想向他显示他的唯一ID,但它保持NULL。我怎么解它?

<script type="text/javascript">
    $('#linkkk').text('Your id is: '+id+'');
</script>               

你需要在成功回调中首先设置id。

$.ajax({
    type: 'POST',
    url: requestUrl,
    data: $(".defaultRequest").serialize(),
    dataType: 'json',
    success: function(data) {
        if(data.response){
            $('div.errormsg').remove();
            if (data.step) {
                openStep(data.step);
            } else {
                openStep('next');
            }
            $('#linkkk').text('Your id is: ' + data.id);
        } else {
            $('div.errormsg').remove();
            $('<div class="errormsg">'+data.message+"</div>").insertBefore(form);
        }
    }
});