全局使用jQuery中的变量

时间:2017-01-30 16:18:52

标签: javascript jquery

我正在尝试使用登录的用户名作为全局变量,可以传递给内部表单,有没有办法可以将登录的用户名变量传递给其他函数,如下所示: 有人可以帮忙:

Fiddle

JavaScript的:

    $(function() {

      function userLogin() {

        userName = $('#user').val();
        var password = $('#pass').val();
      }

     function requestDemo() {
        var userName = userName; // This should inherit the username from login function?
    }

});

3 个答案:

答案 0 :(得分:2)

您只需插入

即可
var globalUsername = ''
var globalPass = '';

就在下面:

$(function() {
像这样:

$(function() {
var globalUsername = ''
var globalPass = '';
...

或以上,像这样:

var globalUsername = ''
var globalPass = '';
$(function() {
...

之后,您可以在脚本中的每个位置使用这两个变量。

答案 1 :(得分:0)

我们已经有了一个全局变量。我们如何简单地在jQuery对象上创建变量。然后,在我们的各种功能中,我们可以简单地检查它们是否存在,如果它们不存在则实施它们,或者如果它们存在则引用它们?

看着你的小提琴,这也可以毫不费力地完成 - 只需让每个需要访问全局命名空间的函数创建或引用它。

$(function() {
  $.bob_jsNS = $.bob_jsNS || {};

  var mainurl = 'http://localhost:9000';

  /* Login User */
  $("#submit-login").click(function() {

    userLogin();
  });

  function userLogin() {
    $.bob_jsNS.user = $.bob_jsNS.user || {};
    var myUser = $.bob_jsNS.user;

    myUser.userName = $('#user').val();
    var password = $('#pass').val();

    // var data = $("#login-user").serialize();

    var data = {
      "userName": myUser.userName
    }

    $('#sdlc-gif').show();

    // I simply reference the global namespace to get the userName.
    if (myUser.userName != '' && password != '') {
    // Removed for clarity. There was code here.
    }
  }


  function requestDemo() {
    /*****
     * Any time I need access to any part of the global namespace,
     *  I should first check that it's there -- and if not, create it.
     *  These first few lines happen on any function that will need to get
     *  or set global values or functions.
     ***/
    $.bob_jsNS = $.bob_jsNS || {};
    $.bob_jsNS.user = $.bob_jsNS.user || {};
    var myUser = $.bob_jsNS.user;
    // If we don't have a username, set it blank.
    //  May not be an issue, but it may break otherwise.
    var myUser.userName = myUser.userName || "";
    var myUser.first = ('#firstName').val();
    var myUser.last = ('#lastName').val();
    var myUser.title = ('#jobTitle').val();
    var myUser.email = ('#email').val();
    var myUser.comments = ('#comments').val();
    var myUser.app1 = ('#app1').val();
    var myUser.app2 = ('#app2').val();
    var myUser.app3 = ('#app3').val();

    var data = {
      "userName": myUser.userName,
      "firstName": myUser.first,
      "lastName": myUser.last,
      "jobTitle": myUser.title,
      "email": myUser.email,
      "comments": myUser.comments,
      "app1": myUser.app1,
      "app2": myUser.app2,
      "app3": myUser.app3
    }
    /***
     * More code was here, abbreviated for clarity.
     ***/ 
  }


  /* Provide feedback form */

  $("#submit-feed").click(function() {

    provideFeedback();
  });
  /* Provide Feedback */
  function provideFeedback() {
    // var data = $("#provide-feed").serialize();
    $.bob_jsNS = $.bob_jsNS || {}
    $.bob_jsNS.user = $.bob_jsNS.user || {};


    var userName = $.bob_jsNS.user.userName || "";
    var q1 = $("input[name='group1']:checked").val();
    var q2 = $("input[name='group2']:checked").val();
    var q3 = $("input[name='group3']:checked").val();
    var q4 = $("#ans4").val();
    var q5 = $("#ans5").val();

    var data = {
      "userName": userName,
      "q1": q1,
      "q2": q2,
      "q3": q3,
      "q4": q4,
      "q5": q5
    }
  }
});

答案 2 :(得分:0)

我建议使用localStoragesessionStorage,具体取决于您对持久性的需求。您可以详细了解两个here的差异。

使用以下方法,您可以将用户名值分配给Web浏览器本地存储,然后在需要时从本地存储中检索该值,并且您不必担心范围太大你的变量放在哪里。

$(function() {

      function userLogin() {

        localStorage.setItem('username', $('#user').val());
        var password = $('#pass').val();
      }

     function requestDemo() {
        var userName = localStorage.getItem('username');
    }

});