简体   繁体   English

如何在以下情况下将数组从smarty模板分配给javascript,并在javascript中访问相同的分配数组?

[英]How to assign array from smarty template to javascript and access the same assigned array in javascript in following scenario?

I've an array titled $all_states assigned to smarty template as follows: 我有一个名为$all_states的数组,分配给smarty模板,如下所示:

Array
(
    [0] => Array
        (
            [id] => 1
            [state_name] => Alabama
            [abbreviation] => AL
            [created_at] => 1398342353
            [updated_at] => 1398342353
        )

    [1] => Array
        (
            [id] => 2
            [state_name] => Alaska
            [abbreviation] => AK
            [created_at] => 1398342353
            [updated_at] => 1398342353
        )

    [2] => Array
        (
            [id] => 3
            [state_name] => Arizona
            [abbreviation] => AZ
            [created_at] => 1398342353
            [updated_at] => 1398342353
        )

    [3] => Array
        (
            [id] => 4
            [state_name] => Arkansas
            [abbreviation] => AR
            [created_at] => 1398342353
            [updated_at] => 1398342353
        )

    [4] => Array
        (
            [id] => 5
            [state_name] => California
            [abbreviation] => CA
            [created_at] => 1398342353
            [updated_at] => 1398342353
        )

    [5] => Array
        (
            [id] => 6
            [state_name] => Colorado
            [abbreviation] => CO
            [created_at] => 1398342354
            [updated_at] => 1398342354
        )
)

I've a jQuery code on same page as follows: 我在同一页面上有一个jQuery代码,如下所示:

$(document).ready(function() {
  window.prettyPrint() && prettyPrint();

  /*Actually I want to use the values from the above array into below loop*/
  for (var i = 1; i <= 100; i++) {
    $('#example28').append('<option value="' + i + '">' + i + '</option>');
  }

    $('#example28').multiselect({
      includeSelectAllOption: true,
      enableFiltering: true,
      maxHeight: 150
    });
  });

You can see from the comment in above code that I want to parse the above array in smarty into the javascript for loop. 您可以从以上代码中的注释中看到,我想将以上数组巧妙地解析为javascript for循环。 How should I achieve this? 我应该如何实现呢? I've to acheve this anyhow so is anyone here who can help me in this regard? 无论如何,我必须做到这一点,所以在这方面可以帮助我的任何人吗? Thanks in advance. 提前致谢。 If you still need any more information regarding my issue I can provide you the same. 如果您仍然需要有关我的问题的更多信息,我可以为您提供同样的信息。

Try 尝试

$(document).ready(function() {
window.prettyPrint() && prettyPrint();

var myPhpArray = <?php echo json_encode($all_states); ?>; // <-- HERE'S YOUR ARRAY

/*Actually I want to use the values from the above array into below loop*/
for (var i = 1; i <= myPhpArray.length; i++) {
    $('#example28').append('<option value="' + i + '">' + i + '</option>');
}

$('#example28').multiselect({
    includeSelectAllOption: true,
    enableFiltering: true,
    maxHeight: 150
});
});

Thing is, you can't pass an array from php to js directly.. I don't know much about smarty so see it as a more generic approach of how it's done. 事实是,您不能将数组直接从php传递给js。.我对smarty不太了解,因此将其视为一种更通用的方法。

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

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