1

在下面的 PHP 代码中,我想从我的 jQuery 脚本访问存储在我的 $val 变量中的值,以便能够发送一个 ajax 调用。对于每个表行,$val 将包含唯一值,我需要访问唯一值才能发送到我的发布请求。

             <?php 
                if($batch_query != null){
                  $i = 0;  
                  foreach($batch_query->result_array() as $row){
                    $val = "'".$row['course_id'].",".$row['center_id'].",".$row['batch_id']."'";//These values are coming from the server side and I need to send it to the controller for the Ajax. 
                    echo "<tr>";
                      echo "<td>".$row['course_name']."</td>";
                      echo "<td>".$row['center_name']."</td>";
                      echo "<td>".$row['batch_name']."</td>";
                      echo "<td>"."<button id= \"btnnumber_$i\"  class='btn info toggler' value=$val>Show Info  <i class='icon-arrow-down'></i></button>"."</td>";// here I am using the value attribute and assigning it the value $val.
                  }
                 } 
                   ?>

这是我的 jQuery

       $(function(){
          $.post('/path /to/ my /controller', {value: $val}).done(function(data){ 

        }); 
       });

        How to get those values in the $val ?

任何帮助将不胜感激。

4

7 回答 7

6

jquery中更好的方法是..

$(function(){ 
  var btnvalue=$('button.info').attr('value');
   $.post('/path /to/ my /controller', {value: btnvalue}).done(function(data){ 

    }); 
});

最简单的(如果你有单独的 .js 文件,这是不可能的)......

var btnvalue= <?php echo $val ?>
 $.post('/path /to/ my /controller', {value: btnvalue}).done(function(data){ 

    }); 
于 2013-03-13T09:04:03.273 回答
2

我会更改按钮,以便将其$val放入数据属性中,如下所示:

"<button id=\"btnnumber_" + $i +"\"  class=\"btn info toggler\" data-value=\"" + $val + "\">Show Info  <i class=\"icon-arrow-down\"></i></button>"

然后我会将您的 jquery 更改为类似

$('button').click(function() {
  $.post('/path /to/ my /controller', 
     {value: $(this).data('value')}).done(function(data){ 

  }); 
});

这是按钮单击获取数据值属性的小提琴示例:http: //jsfiddle.net/T7cJR/

于 2013-03-13T09:18:42.460 回答
1

将它们作为 javascript 变量写在页面上。

于 2013-03-13T09:00:59.013 回答
1

如果您的 jquery 在同一页面中,则可以使用以下内容。

    $(function(){
        $.post('/path /to/ my /controller', 
         {value: <?php echo   $val?>}).done(function(data){ 

     }); 
    });
于 2013-03-13T09:04:23.037 回答
1

像这样使用它:<?php echo $val; ?>

$.post('/path /to/ my /controller', {value: <?php echo $val; ?>}).done(function(data){

您需要在此行之前为该变量赋值。

于 2013-03-13T09:04:29.183 回答
1

编写代码以获取 $var 变量中的所有值。然后在脚本标签内的页面末尾,写下这个

$(function(){
          $.post('/path /to/ my /controller', {value: <?php echo $val ?>}).done(function(data){ 

          }); 
});
于 2013-03-13T09:05:15.883 回答
1

您可以将数据保存为 tr 属性,然后使用 jquery 访问它:

<?php 
 if($batch_query != null){
    $i = 0;  
    foreach($batch_query->result_array() as $row){
       $val = "'".$row['course_id'].",".$row['center_id'].",".$row['batch_id']."'";//These values are coming from the server side and I need to send it to the controller for the Ajax. 
       echo "<tr id='".$val."'>";
        ...        
于 2013-03-13T09:09:46.147 回答