如何将json数据从一个javascript函数传递到另一个javascript函数?

如何将json数据从一个javascript函数传递到另一个javascript函数?,javascript,jquery,ruby-on-rails-4,Javascript,Jquery,Ruby On Rails 4,JS+Jquery+rails4 一,。view.html.erb <%= select_tag "schedule[id]", options_for_select(@travel_names), {:multiple => true, :onchange => "renderSchedules(#{raw @schedule_hash.to_json},#{raw @deals_hash.to_json});" } %> @schedule\u hash=insta

JS+Jquery+rails4

一,。view.html.erb

<%= select_tag "schedule[id]", options_for_select(@travel_names), {:multiple => true, :onchange => "renderSchedules(#{raw @schedule_hash.to_json},#{raw @deals_hash.to_json});" } %>
@schedule\u hash=instance, @deals\u hash=实例 ,以json格式传递给js函数

二,。webapp.js

function renderSchedules(json_data, deals_hash){
   new_json_data = filterSchedule(json_data, deals_hash);
   //More Code
}

function filterSchedule(json_data, deals_hash) {
//More Code
 $('#active_fiters_blk').append('<a href="#" id="active_travel_filter" style="color:#F26F21;margin-right:3%;" value="'+operator_ids[i]+'" onclick="removeTravelFilter('+json_data+','+deals_hash+')">"Link"</a>');
// Here i am getting json_data = [object Object], deal_hash = [object, Object] on console , i need to pass json data to removeTravelFilter function
}

function removeTravelFilter(jd, dh){
  //Some COnditions
  renderSchedules(jd, dh);
  // From here also i need to pass json data to renderSchedule function.
}

您需要对json_数据进行字符串化,因为它是一个对象。对象上的ToString方法返回字符串[Object]。您需要使用:

onclick="removeTravelFilter('+JSON.stringify(json_data)+','+JSON.stringify(deals_hash)+')">

最好不要使用内联脚本:

function filterSchedule(json_data, deals_hash) {
    $('<a href="#" id="active_travel_filter" style="color:#F26F21;margin-right:3%;" value="' + operator_ids[i] + '">Link</a>').on('click', function () {
        removeTravelFilter(json_data, deals_hash);
    }).appendTo('#active_fiters_blk');
}

也就是说,ID在文档上下文中必须是唯一的。看起来您的代码正在复制ID。

您有没有收到任何错误?为什么使用内联脚本???没有错误,但在传递json_数据时,它在控制台上显示[object object]。@A.Wolff我尝试了同样的结果:不,我有唯一的id,我删除了后缀,因为我需要让所有人都能理解代码。这不是。附加到DIV如果DIV是“active\u fiters\u blk”,它应该附加链接。控制台中有错误吗?编辑:我明白了,缺少双引号,更新了答案实际上我想要每个链接的内联脚本,但若我给出这个,那个么链接的构造就错了?