簡體   English   中英

如何將參數傳遞給jQuery函數

[英]How pass a parameter to jQuery function

在這里,我使用此代碼使用JSON創建HTML表,在我的Web程序中,我使用PHP生成了JSON ,現在我需要將JSON作為參數傳遞給上述函數,我該怎么做。

<script type="text/javascript">
    var data = [
  {
    "UserID": 1,
    "UserName": "rooter",
    "Password": "12345",
    "Country": "UK",
    "Email": "sac@gmail.com"

  },
  {
    "UserID": 2,
    "UserName": "binu",
    "Password": "123",
    "Country": "uk",
    "Email": "Binu@gmail.com"
  },
  {
    "UserID": 3,
    "UserName": "cal",
    "Password": "123",
    "Country": "uk",
    "Email": "cal@gmail.com"
  },
  {
    "UserID": 4,
    "UserName": "nera",
    "Password": "1234",
    "Country": "uk",
    "Email": "nera@gmail.com"
  },
  {
    "UserID": 4,
    "UserName": "nera",
    "Password": "1234",
    "Country": "uk",
    "Email": "nera@gmail.com"
  }
];
$(document).ready(function () {
    var html = '<table class="table table-striped">';
    html += '<tr>';
    var flag = 0;
    $.each(data[0], function(index, value){
        html += '<th>'+index+'</th>';
    });
    html += '</tr>';
     $.each(data, function(index, value){
         html += '<tr>';
        $.each(value, function(index2, value2){
            html += '<td>'+value2+'</td>';
        });
        html += '<tr>';
     });
     html += '</table>';
     $('body').html(html);
});
</script>

我可以使用PHPJSON值作為參數傳遞給此函數嗎

您可以使用php標簽作為參數傳遞

   <?php
$data = '[
  {
    "UserID": 1,
    "UserName": "rooter",
    "Password": "12345",
    "Country": "UK",
    "Email": "sac@gmail.com"

  },
  {
    "UserID": 2,
    "UserName": "binu",
    "Password": "123",
    "Country": "uk",
    "Email": "Binu@gmail.com"
  },
  {
    "UserID": 3,
    "UserName": "cal",
    "Password": "123",
    "Country": "uk",
    "Email": "cal@gmail.com"
  },
  {
    "UserID": 4,
    "UserName": "nera",
    "Password": "1234",
    "Country": "uk",
    "Email": "nera@gmail.com"
  },
  {
    "UserID": 4,
    "UserName": "nera",
    "Password": "1234",
    "Country": "uk",
    "Email": "nera@gmail.com"
  }
]';


?>

<script>
    $(document).ready(function () {
    var html = '<table class="table table-striped">';
    html += '<tr>';
    var flag = 0;

    var data2   =   <?php echo $data; ?>;
    $.each(data2[0], function(index, value){
        html += '<th>'+index+'</th>';
    });
    html += '</tr>';
     $.each(data2, function(index, value){
         html += '<tr>';
        $.each(value, function(index2, value2){
            html += '<td>'+value2+'</td>';
        });
        html += '<tr>';
     });
     html += '</table>';
     $('body').html(html);
     console.log(html);
});
    </script>

我建議只在php中創建表,以便在加載時在頁面上,這對於seo和用戶體驗會更好。

但這是從php加載頁面時無需發出ajax請求即可打印javascript數據的方式。

關鍵是<?= json_encode(array); ?> <?= json_encode(array); ?> json編碼會將php數組/關聯數組解析為json對象。

我在單個腳本標簽中進行了此操作,以防解析或您的編輯器語法出錯。

<?php                                                                                                             
$data = [                                                                                                         
  [ "UserID" => 1, "UserName" => "rooter", "Password" => "12345", "Country" => "UK", "Email" => "sac@gmail.com" ],
  [ "UserID" => 2, "UserName" => "binu", "Password" => "123", "Country" => "uk", "Email" => "Binu@gmail.com" ],   
];                                                                                                                
?>                                                                                                                
<script>data = <?= json_encode($data, JSON_NUMERIC_CHECK); ?></script>                                                                
$(document).ready(function () {                                                                                   
    var html = '<table class="table table-striped">';                                                             
    html += '<tr>';                                                                                               
    var flag = 0;                                                                                                 
    $.each(data[0], function(index, value){                                                                       
        html += '<th>'+index+'</th>';                                                                             
    });                                                                                                           
    html += '</tr>';                                                                                              
     $.each(data, function(index, value){                                                                         
         html += '<tr>';                                                                                          
        $.each(value, function(index2, value2){                                                                   
            html += '<td>'+value2+'</td>';                                                                        
        });                                                                                                       
        html += '<tr>';                                                                                           
     });                                                                                                          
     html += '</table>';                                                                                          
     $('body').html(html);                                                                                        
});                                                                                                               
</script>                                                                                                         

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM