jquery - 你如何在ajax中创建一个变量,对函数的其余部分是全局的
问题描述
我正在尝试通过 jquery 中的数据库获取一些数据。我通过 ajax 执行此操作,ajax 代码返回值很好。然后我用数据设置一个变量comment
。
然后使用注释变量将 html 插入到元素中。但是,这个变量似乎没有设置,因为它没有通过 ajax 函数传递。我该如何做到这一点?
$(document).on('click', '.customer_progress_edit', function(){
timeline_id = $(this).attr('data-timeline');
$.ajax({
type : 'POST',
url : '//'+base_url+'/ajax2/timeline-comment.php',
data : 'timeline_id='+timeline_id,
success : function(data) {
comment = data;
}
});
$('#customer_pop_edit_comment').val(comment);
});
是的,我知道我可以简单地在 ajax 函数中使用 html() 函数,但我已经简化了代码,以便更容易地了解我想要实现的目标,它不仅仅是这个。
解决方案
try this
var get_data = $.ajax({
type : 'POST',
url : '//'+base_url+'/ajax2/timeline-comment.php',
data : 'timeline_id='+timeline_id
});
get_data.done(function(html){
comment = html;
});
推荐阅读
- c - 删除列表中的奇数 C
- symfony - Symfony4 针对不同环境的不同安全配置
- node.js - Jenkins 使用旧节点版本构建项目
- azure-devops - 是否有单独的 VSTS 代理用于构建和发布?
- java - 如何将 StackTraceElement[] 转换为字符串?
- arrays - 如何仅获取 index[0] 的值以显示 JSON 对象中的数组?
- sql - SQL 查找更新了哪些表。
- javascript - 我们如何修复窗口滚动顶部的按钮?
- python - 基于多列的值元组过滤熊猫
- javascript - 如何在没有新请求的情况下修改 ajax src 属性?