普通网友 2013-01-28 18:38
浏览 9
已采纳

通过javascript获取价值[关闭]

i m use php which print my table in which first column is sr.no but in which i want to store value of exam_id. how can i get this Exam_id value by javascript

        foreach ($data as $key) {
            echo "<tr>";
            print'
<td class="contact" id="exam_id" name=' . $key['exam_id'] ." width="5%" value='.$key['date'].'>' . $i . '</td>
<td class="contact"  width="20%">' . $key['date'] . '/' . $key['month'] . '/' . $key['year'] . '</td>
<td class="contact"  width="30%">' . $key['Title'] . '</td>
<td class="contact"  width="20%">' . $key['Branch'] . '</td>
<td class="contact"  width="6%">' . $key['Sem'] . '</td>
<td class="contact" width="24%" ><a onclick="javascript:create_result();" href="#">Create Result</a></td>';

            $i++;
            echo "</tr>";
        }

javacript is

<script type="text/javascript">
    function create_result() {

 var exam_id = document.getElementById('exam_id').value



      $.ajax({type:'POST', url: 'result/create_result',data:exam_id, success: function(response) {
              $('#content').html(response);
   }});
    return false;
}
</script>
  • 写回答

1条回答 默认 最新

  • dongpo2340 2013-01-28 19:04
    关注

    Firstly you need to clean up your PHP code. The syntax is a bit off, and is very hard to read.

    Once you have managed to get the PHP to output the HTML correctly you need to change to JavaScript you are using to get the exam_id value.

    As @MarcB said, you have duplicate IDs in your HTML, since you will be running the PHP snippet multiple times as it is a foreach loop. That will result in you having many cells with the id exam_id.

    You need to be able to reference a specific cell in order to get the value of exam_id. Since the id of the cell in question is not unique, it should be converted to a class. <td class="contact exam_id" name... </td>

    Then you need to find a way to uniquely identify each row. This could be done by attaching the exam_id of each row into the row (tr) declaration like so, "<tr id='exam-" . $key['exam_id'] . "' >"; (This would replace line 2 of your PHP)

    Now you have to find a way of identifying which row you wish to have as your context before you can select the value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?