将PHP函数的值赋给jquery

时间:2014-08-24 03:04:24

标签: javascript php jquery

我有3个页面如下:Tasks.php(显示数据)从tasks-functions.php(运行代码)和运行脚本的(tasks.js)获取数据。 在tasks.js中 - 我有一个点击功能如下:

  $(document).on("click", "#mybtn", function () {
    var val1 = $('#taskid').val();
    alert($(val1).val());
    $('#EditTaskModal').modal('show');
  $(".table table-striped table-advance table-hover #test").html(val1);
    });

(#taskid)位于tasks-functions.php中,这是一个echo,它通过tasks.php /

上的id将数据显示到表中

tasks.js无法获取要分配给val1的值。

任务-功能:

function loadUserTasks(){
    $myTasksUser = $_SESSION['username'];
    $myTasks = mysql_query("SELECT * FROM tasks WHERE taskowner = '$myTasksUser' && taskstatus = 'Active'");
        while ($rows = mysql_fetch_array($myTasks)){
            $this->taskID = $rows['taskid'];
            $this->taskTitle = $rows['tasktitle'];
                $this->taskDetail = $rows['taskdetail'];
                $this->taskType = $rows['tasktype'];
                $this->taskResource = $rows['taskresource'];
                $this->taskStatus = $rows['taskstatus'];

                //Change icon color and value for task status
                if ($this->taskStatus == 'completed') {
                    $this->taskStatus = '<span class="label label-info label-mini">Completed</span>';
                }
                else{
                    $this->taskStatus = '<span class="label label-warning label-mini">Open</span>';
                }

                echo "  
                                      <tr>                          
                                  <td><a href=\"#\" id=\"taskid\" data-toggle=\"modal\">$this->taskTitle </a></td>
                                  <td>$this->taskDetail</td>
                                  <td>$this->taskResource</td>
                                  <td>$this->taskStatus</td>
                                  <td><button id=\"mybtn\">Edit</button></td>
                                  <td>
                                      <div>
                                        <form id=\"completetaskform\"  method=\"post\" >
                                            <input type=\"hidden\" name=\"taskid\" value=\"$this->taskID\" />
                                             <input type=\"hidden\" name=\"acct\" value=\"$this->taskResource\" />
                                             <div class=\"pull-right chat-features\">
                                          <button class=\"btn btn-success btn-xs\" id=\"completeTask\"><i class=\"icon-ok\"></i></button>
                                         </form> 
                                        </div>           
                                      <button class=\"btn btn-primary btn-xs\"><i class=\"icon-pencil\"></i></button>
                                      <button class=\"btn btn-danger btn-xs\"><i class=\"icon-trash \"></i></button>
                                  </td>
                              </tr>";

                          } 
   }

tasks.php:

                  <table class="table table-striped table-advance table-hover" id="tasksTable">
                      <thead>
                      <tr>
                          <th><i class="icon-bullhorn"></i> Title</th>
                          <th class="hidden-phone"><i class="icon-question-sign"></i> Descrition</th>
                          <th><i class="icon-bookmark"></i> Related To</th>
                          <th><i class=" icon-edit"></i> Status</th>
                          <th><i class=" icon-edit"></i> Action</th>
                      </tr>
                      </thead>
                      <?php $var = new task; $var->loadUserTasks(); ?>
                      <tbody>
                      <!-- get data from tasks_functions.php -->
                      </tbody>
                  </table>

我希望我足够解释,我不确定这是否可行,因为涉及3页。

2 个答案:

答案 0 :(得分:0)

如果要将该变量传递给Javascript,可以执行以下操作:

<table class="table table-striped table-advance table-hover" id="tasksTable">
    <thead>
    <tr>
        <th><i class="icon-bullhorn"></i> Title</th>
        <th class="hidden-phone"><i class="icon-question-sign"></i> Descrition</th>
        <th><i class="icon-bookmark"></i> Related To</th>
        <th><i class=" icon-edit"></i> Status</th>
        <th><i class=" icon-edit"></i> Action</th>
    </tr>
    </thead>
    <?php $var = new task; $var->loadUserTasks(); ?>
    <tbody>
    <!-- get data from tasks_functions.php -->
    </tbody>
</table>
<script>
  // Declare a global variable to be used by your JavaScript
  var taskId = <?php echo $var->taskID; ?>;
 </script>

然后,在您的JavaScript中,您可以这样做:

$(document).on("click", "#mybtn", function () {
    // Use your global taskId variable as an #id query
    var val1 = $('#' + taskId).val();
    alert($(val1).val());
    $('#EditTaskModal').modal('show');
    $(".table table-striped table-advance table-hover #test").html(val1);
});

您可以使用jQuery查询表并获取适当的值,而不是将PHP中的变量传递到您的javascript中:

$(document).on("click", "#mybtn", function () { 
    // Query the DOM for our table
    var $table = $(".table.table-striped.table-advance.table-hover");
    // Find the first input:hidden in the DOM (it would be better to add a class
    // or an ID that input and query it using that
    var val1 = $table.find('form input:hidden').first().val();
    alert($(val1).val());
    $('#EditTaskModal').modal('show');
    // Find something with the test id in our table
    $table.find("#test").html(val1); // This #test doesn't seem to exist
});

答案 1 :(得分:0)

由于您有一个while循环并且正在打印多个锚点,因此您必须向锚点添加一个类并在此类上绑定click事件。事件处理程序可以通过'this'访问元素。

我要添加一个名为'.taskAnchor'的类。你的代码可能是这样的:

$(document).on("click", ".taskAnchor", function () {
    var val1 = $(this).text();
    //... your handler code here
});

你的php echo必须用类打印锚点:

<td><a href=\"#\" class=\"taskAnchor\" data-toggle=\"modal\">$this->taskTitle </a></td>