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

时间:2015-03-06 07:39:15

标签: javascript jquery html

$(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?

的值从此页面设置它的值

2 个答案:

答案 0 :(得分:0)

  

如何使用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)) : "";

答案 1 :(得分:0)

我认为在attendee.html中的某个地方存在类似:

var UID_USER = "Some Value";

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

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

结帐load功能