[英]Having 2 pages, How to change the content of a second page by a click event on a button in the first one?
Here is my problem, I have a page called page1 (I have a button in this page to access another page called page2). 这是我的问题,我有一个名为page1的页面(此页面中有一个按钮可以访问另一个名为page2的页面)。 I want to change the content of a specific selector in page2 by a click event on the button of page1.
我想通过page1的按钮上的click事件更改page2中特定选择器的内容。 Here is my code for page 1 in pug
这是我的帕格第1页的代码
form#abandonForm(name='btn' action='page2')
button#abandonBtn.button Abandonner
Here is my code for page2 in pug 这是我在哈巴狗中page2的代码
section#termineExam
and here is my .js file 这是我的.js文件
$( document ).ready(function() {
var $abandonBtn = $('#abandonBtn');
$abandonBtn.click(function(){
$.get('page2', null, function(data){
var $data = $(data);
console.log(data);
console.log($data.find('#termineExam').html());
$data.find('#termineExam').text("section changed");
console.log($data.find('#termineExam').html()); // print the changes
console.log(data); // print the same data without changes on the section
})
});
});
I don't understand why changes are applied on the DOM but not on the page2 itself. 我不明白为什么将更改应用于DOM而不应用于page2本身。 Thank you for helping and sorry for my English!
感谢您的帮助,对不起我的英语!
You seem to misunderstand how jQuery.get()
works. 您似乎误解了
jQuery.get()
工作方式。
From the docs: 从文档:
jQuery.get( url [, data ] [, success ] [, dataType ] )
jQuery.get(url [,data] [,success] [,dataType])
Returns: jqXHR
返回值:jqXHR
Description: Load data from the server using a HTTP GET request.
说明:使用HTTP GET请求从服务器加载数据 。
Basically, you are getting the content from 'page2' as a string and storing it in a variable named data
. 基本上,您是从'page2'中获取内容作为字符串并将其存储在名为
data
的变量中。 This is a one way street. 这是单行道。 When you call
$data.find('#termineExam').text("section changed");
当您调用
$data.find('#termineExam').text("section changed");
you are modifying the local variable not the actual page2 file 您正在修改本地变量, 而不是实际的page2文件
When you navigate to page2
, you are requesting a brand new copy of the page2
file without any of the modifications that you applied to that string over on page1. 当导航到
page2
,您请求的是page2
文件的全新副本,而没有在page1上对该字符串应用的任何修改。
If you want to actually modify the content of page2
, you'll need some sort of server side script (like php) that accepts new data from page1 and updates the page2 file. 如果要实际修改
page2
的内容,则需要某种服务器端脚本(例如php),该脚本接受page1的新数据并更新page2文件。
Alternatively, if you just want to send some data to page 2 and have that data appear when that page loads, there are many ways you could do that. 另外,如果您只想向第2页发送一些数据并在该页面加载时显示该数据,则可以采用多种方法。
localstorage
: localstorage
: Make a file called storage.js
with the below content and include this file in both page1
and page2
创建一个具有以下内容的名为
storage.js
的文件,并将此文件同时包含在page1
和page2
/**
* Feature detect + local reference for simple use of local storage
* if (storage) {
* storage.setItem('key', 'value');
* storage.getItem('key');
* }
*
*/
var storage;
var fail;
var uid;
try {
uid = new Date;
(storage = window.localStorage).setItem(uid, uid);
fail = storage.getItem(uid) != uid;
storage.removeItem(uid);
fail && (storage = false);
} catch (exception) {}
/* end Feature detect + local reference */
In page1
do this: 在第1
page1
执行以下操作:
$(document).ready(function() {
var $abandonBtn = $('#abandonBtn');
$abandonBtn.click(function() {
if(storage){
storage.setItem('termineExam', 'section changed');
}
});
});
Then, in page2
do this: 然后,在
page2
做到这一点:
$(document).ready(function() {
if(storage){
var text = storage.getItem('termineExam');
if(text) $('#termineExam').text(text);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.