javascript - jQuery var 没有改变

标签 javascript jquery

我有一个 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);
        }
    }
});

关于javascript - jQuery var 没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17060495/

相关文章:

javascript - 如何在 Chrome 控制台窗口中开始一个新行?

javascript - D3 : how to disable zoom on icons with background image?

javascript - 在几个相似的 div 之间调用该 div 内的函数时访问该 div 内的元素

javascript - AngularJS 如何统计 jQuery 动态添加的字段?

php - jQuery Ajax 表单仅发送几个复选框值中的最后一个

javascript - 如何让 jQuery 动画只工作一次?

javascript - 在 JavaScript 中转义 json

javascript - 为什么日期选择器使用 Jquery 日期选择器只显示 20 年的选项?

javascript - jquery 查找文本框

javascript - 如何计算对话框中文本区域的字符数?