通过JavaScript将php变量传递到另一个php页面

Jah*_*wan 1 html javascript php jquery

我想id在点击整行时将其传递到下一页.我试图自己做,但我无法这样做.我的代码如下:

$( "#tablerow" ).click(function() {
  var jobvalue=$("#jobid").val();
  alert(jobvalue);
  window.location.href = "jobsview.php?id=" + jobvalue;
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
   <tr id="tablerow">
    <td><?=$srno?></td>
    <td id="jobid"><?=$row['ID']?></td>
   </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

Sam*_*mir 5

val()方法适用于表单元素等input,select等.

使用text()方法,

var jobvalue = $("#jobid").text();
Run Code Online (Sandbox Code Playgroud)

更新

一个HTML只能有一个ID整个文档.要为多个元素启用单击事件并将单击的元素传递到另一个页面,请将ID属性更改为class.

<table>
  <tbody>
    <tr class="tablerow" >
     <td><?=$srno?></td>
     <td class="jobid"><?=$row['ID']?></td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

然后您可以JS按如下方式点击,

$( ".tablerow" ).click(function() {
   /**  $(this) will refer to current tablerow clicked
     *  .find(".jobid") will find element with class `jobid`
     *  inside currently clicked tablerow
   */
   var jobvalue = $(this).find(".jobid").text();
   alert(jobvalue);
   window.location.href = "jobsview.php?id=" + jobvalue;
});
Run Code Online (Sandbox Code Playgroud)