简体   繁体   English

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

[英]How to pass json data from one javascript function to another javascript function?

JS + Jquery + Rails 4 JS + jQuery + Rails 4

1.) view.html.erb 1.)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_hash = instance, @deals_hash = instance , passing to the js function as json format. @schedule_hash =实例,@deals_hash = instance,以json格式传递给js函数。

2.) webapp.js 2.)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.
}

You need to stringify your json_data because it is an Object. 您需要对json_data进行字符串化,因为它是一个对象。 The toString method on an Object returns the string "[object Object]" . 对象上的toString方法返回字符串"[object Object]" You need to use: 您需要使用:

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

You'd have better to not use inline script: 您最好不要使用内联脚本:

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');
}

That's said, IDs must be unique on document context. 也就是说,ID在文档上下文中必须是唯一的。 Looks like your code is duplicating IDs. 看起来您的代码正在复制ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Javascript数据从一个函数传递到另一个函数 - Pass Javascript data from one function to another javascript - 将数据从一个函数传递到另一个函数 - javascript - pass data from one function to another 如何在javascript中将变量从一个函数传递给另一个函数 - how to pass variable from one function to another function in javascript 如何在JavaScript中将局部变量从一个函数传递给另一个函数 - how to pass local variable from one function to another function in javascript 如何在原版 JavaScript 中将其从一个 function 传递到另一个 function? - How to pass this from one function to another function in vanilla JavaScript? 如何将参数从一个 function 传递到另一个 function onclick in javascript - How to pass a argument from one function to the another function onclick in javascript javascript将元素从一个函数传递到另一个函数 - javascript pass element from one function to another 如何将json数据从一个JavaScript传递到另一个 - How to pass json data from one javascript to another JavaScript:如何将对象值从一个函数传递给另一个函数 - JavaScript: how to pass object value from one function to another Javascript - 如何通过功能点击,从一个菜单到另一个 - Javascript - How pass function click, from one menu to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM