简体   繁体   English

使用 jQuery 或 Ajax 得到 function 从另一个页面拉入文本

[英]Using jQuery or Ajax get function to pull in text from another page

The short story is our CMS limits us to using solely html/js.简而言之,我们的 CMS 将我们限制为仅使用 html/js。 I'm creating a dynamic graph page that has a lot of data that will be updated monthly.我正在创建一个动态图表页面,其中包含将每月更新的大量数据。 There is a separate php form on the server the staff member will fill out, that content is stored in the database, and there's another php file that displays the information.工作人员将填写的服务器上有一个单独的 php 表格,该内容存储在数据库中,还有另一个 php 文件显示该信息。 Jquery/Ajax needs to pull that info from the outside php page, which I've done on one other project. Jquery/Ajax 需要从外部 php 页面中提取该信息,这是我在另一个项目中完成的。 This difference this time is that I need to target specific sections of code to pull in, because the results will be going into a javascript variable for use in populating a graph.这次的区别在于我需要针对特定的代码部分来引入,因为结果将进入 javascript 变量以用于填充图形。

With me so far?跟我到现在? What I need jquery/ajax to do is target specific div ID's on the populating php page so I can direct it to the proper js variable.我需要 jquery/ajax 做的是在填充的 php 页面上定位特定的 div ID,这样我就可以将它定向到正确的 js 变量。

I've looked at jston, but am not strong with JS, and thus that looks like Greek.我看过 jston,但对 JS 不强,因此看起来像希腊语。 Thoughts, Ideas, Suggestions?想法、想法、建议?

EDIT:编辑:

I've tried this code:我试过这段代码:

$('#hiddenDiv').load('test.php #newdata');

Which imported nothing, even though there is text on the test.php page with a div with the id "newdata".即使 test.php 页面上有一个带有 ID 为“newdata”的 div 的文本,也没有导入任何内容。

You can use the jQuery load function:您可以使用 jQuery load function:

$('#loadIntoMe').load('somepage.php #onlyLoadMe');

If there is a space in the URL provided as a parameter to the load function, it's assumed to be a jQuery selector and is used to extract the specified elements, instead of returning the entire document.如果 URL 中有空格作为参数提供给load function,则假定为 jQuery 选择器,而不是返回整个文档。 See the docs for more information.有关更多信息,请参阅文档

Update (based on comments)更新(基于评论)

To store the result in a variable, you could use a hidden div and a callback function:要将结果存储在变量中,您可以使用隐藏的div和回调 function:

var yourVariable;
$('#loadIntoMe').load('somepage.php #onlyLoadMe', function() {
    yourVariable = $(this).html();
});

All javascript graphing libraries I have employed have required a javascript object to be passed to them.我使用的所有 javascript 图形库都需要将 javascript object 传递给它们。 In this instance I would be use jQuery $.get to get a json_encode d set of data.在这种情况下,我将使用 jQuery $.get来获取json_encode d 数据集。

PHP PHP

<?php
header('Conent-type: application/json');
$array_of_data = array(
    'axes1' => array( // data goes here
    ),
    // etc and so on
);
echo json_encode($array_of_data);
?>

JS JS

$.getJSON('/url/to/php.php', function(data) {
    // console.log(data);
    // generate graph from data
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM