简体   繁体   English

如何将命名空间变量传递给click函数param变量? jQuery的

[英]How to pass namespace variable into click function param variable? jQuery

So I found an awesome solution to get around needing to use Global variables in jQuery here. 所以我找到了一个很棒的解决方案来解决这里需要在jQuery中使用全局变量的问题。 Everywhere I say namespace, originally I was going to use a Global var. 无论我说什么名称空间,最初我都会使用Global var。

However I'm not able to send my namespace variable through a param on a simple click function. 但是,我无法通过简单的单击函数上的参数发送我的命名空间变量。

I want to do this because I have these variables that need to be saved and used to create modal windows as well as to let the app know what dynamically created buttons control what modal. 我想这样做是因为我有这些变量需要保存并用于创建模态窗口以及让应用知道动态创建的按钮控制什么模态。

So below is an example of my namespace object where I first create the variables My jQuery namespace object: 下面是我的命名空间对象的一个​​例子,我首先创建变量My jQuery namespace object:

$.reg = { 
    masterRole : " ", 
    role_ID : " ",
    row_Name : " ",
    ary_Check_Role : []
}; 

$(document).ready(function () {
    ...

As you go through the app the values for those namespace variables get set when you click this button (grabs data from HTML) Variables are set on my roleBtnClicked function 当您浏览应用程序时,单击此按钮时会设置这些命名空间变量的值(从HTML中获取数据)在我的roleBtnClicked函数上设置变量

function roleBtnClicked(event){

    $.reg.masterRole  = $(this).html();            /* Get role name: Actor */
    $.reg.role_ID     = $(this).attr('role');      /* Get role-1 */
    $.reg.rowName     = $(this).attr('row');       /* Get row-role-1 */
    $.reg.blueBtn     = $(this).attr('blue');      /* Get blue-btn-1 */

Now a modal window pops up, after clicking some checkboxes and pushing data into an array, when you click the done button(below) all the variables need to be saved into the namespace vars again. 现在弹出一个模态窗口,点击一些复选框并将数据推入数组后,当您单击完成按钮(下方)时,所有变量都需要再次保存到命名空间变量中。

My done button click Function, where I'm trying to pass my namespace variables via param vars 我的完成按钮单击功能,我试图通过param vars传递我的命名空间变量

$(".doneButton").click(
    {
        param1: $.reg.masterRole,
        param2: $.reg.role_ID,
        param3: $.reg.rowName,
        param4: $.reg.ary_Check_Role
    },
    doneBtnClicked);

function doneBtnClicked(event){
    alert('$.reg.roleName    = '+$.reg.roleName);
    alert('event.data.param1 = '+event.data.param1);

    masterRole= event.data.param1;
    role_ID  = event.data.param2;
    rowName  = event.data.param3;
    ary_Check_Role = event.data.param4;

Note the 2 Alerts above in the click function, the first one will display the correct value, however the 2nd one doesn't display anything. 请注意点击功能中的上述2个警报,第一个将显示正确的值,但第二个不显示任何内容。 Also Having a problem getting the Array to come through as well. 还有一个问题让阵列也通过。

So questions: Should I be doing it this way? 所以问题:我应该这样做吗? How do you pass an Array into a jQuery namespace correctly and then get it passed into a click function param? 如何正确地将Array传递到jQuery命名空间,然后将其传递给click函数参数?

First off, you should avoid putting things into the jQuery object. 首先,你应该避免将东西放入jQuery对象。 Use closures for that. 使用闭包。

Second: You should use HTML data-... attributes and jQuery data() to attach custom properties to HTML elements. 第二:您应该使用HTML data-...属性和jQuery data()将自定义属性附加到HTML元素。 Avoid using non-standard properties. 避免使用非标准属性。

Third: You can use separate named function definitions for event handlers, but it makes most sense when you actually re-use those functions for different elements across your code (you don't seem to do that). 第三:您可以为事件处理程序使用单独的命名函数定义,但是当您在代码中为不同元素重用这些函数时,它们最有意义(您似乎不这样做)。 Using anonymous functions that you pass directly to .click() , for example, results in more understandable code. 例如,使用直接传递给.click()匿名函数可以产生更易理解的代码。

// use this shorthand instead of $(document).ready(function () { ... });
$(function () {

  // variable reg will be available to all functions 
  // defined within this outer function. This is called a closure.
  var reg = { 
    masterRole : " ", 
    role_ID : " ",
    row_Name : " ",
    ary_Check_Role : []
  };

  $(".roleButton").click(function (event) {
    // modify state
    reg.masterRole  = $(this).html();            /* Get role name: Actor */
    reg.role_ID     = $(this).data('role');      /* Get role-1 */
    reg.rowName     = $(this).data('row');       /* Get row-role-1 */
    reg.blueBtn     = $(this).data('blue');      /* Get blue-btn-1 */

    // note that this requires markup like this:
    // <button data-role="foo" data-row="bar" data-blue="baz">

    // ...
  });

  $(".doneButton").click(function (event) {
    // debug output
    alert(JSON.stringify(reg, 2, 2));
  });

});

Use multiple $(function () { ... }); 使用多个$(function () { ... }); blocks to separate things that should be separate. 阻止分开应该分开的东西。

Don't forget to always use var for every variable you declare. 不要忘记始终对您声明的每个变量使用var Variables declared without var will be global - and you don't want global variables. 声明为var变量将是全局变量 - 并且您不需要全局变量。

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

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