简体   繁体   English

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

[英]Pass php variable through JavaScript to another php page

I want to pass the id to the next page on clicking the entire row. 我想在单击整行时将id传递给下一页。 I tried to do it myself but I wasn't able to do so . 我本人尝试这样做,但未能这样做。 my code is below : 我的代码如下:

 $( "#tablerow" ).click(function() { var jobvalue=$("#jobid").val(); alert(jobvalue); window.location.href = "jobsview.php?id=" + jobvalue; }); 
 <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> 

val() method works for form elements like input , select etc. val()方法适用于表单元素,例如inputselect等。

Use text() method, 使用text()方法,

var jobvalue = $("#jobid").text();

Update 更新资料

An HTML can have only one ID throughout the document. HTML在整个文档中只能有一个ID To enable click event for multiple elements and pass the one that is clicked onto another page, change ID attribute to class . 要为多个元素启用click事件并将被单击的元素传递到另一页面,请将ID属性更改为class

<table>
  <tbody>
    <tr class="tablerow" >
     <td><?=$srno?></td>
     <td class="jobid"><?=$row['ID']?></td>
    </tr>
  </tbody>
</table>

Then you can get the once clicked in JS as follows, 然后,您可以按如下所示在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;
});

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

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