将 var 的值应用于加载页面中的另一个变量

Apply value of var to another var in loaded page

本文关键字:另一个 变量 加载 var 应用于      更新时间:2023-09-26
$(document).ready(function(){
    $('#id_button').click(function(){
        if ($('#id_text').val()==='')
            alert('Enter Your Id to proceed');
        else {
            $('body').load('attendee.html');
            alert(UID_USER);
        }
    });
});

这里的变量UID_USERattendee.html 的变量。如何从此页面设置它的值为 id_text?

如何从此页面设置其值id_text?

以某种方式将值传递给attendee.html,也许是在查询字符串上:

$(document).ready(function(){
    $('#id_button').click(function(){
        var id = $('#id_text').val();
        if (id ==='')
            alert('Enter Your Id to proceed');
        else {
            $('body').load('attendee.html?' + encodeURIComponent(id));
        }
    });
});

attendee.html页上的代码将在页面加载时location.search查找查询字符串信息。 并将其分配给UID_TEXT

// Get the id from the query string, skipping the ? character
UID_TEXT = location.search ? decodeURIComponent(location.search.substring(1)) : "";

我假设在与会者.html的某个地方存在类似的东西:

var UID_USER = "Some Value";

在这种情况下,只有在加载页面后才能访问此值;

$('body').load('attendee.html', function(){
  alert(UID_USER);
});

结帐加载功能