1

这是我第一次来这里。感谢您有机会成为这个社区的一员。

我有一个 PHP 代码,它使用来自 mySQL 数据库的数据生成一个表。它将列出:

$output .='<tr id="'.$id.'">';
$output .='<td>'.$agency.'</td>';
$output .='<td>'.$contact.'</td>';
$output .='<td align="left">'.$telephone.'</td>';
$output .='<td align="left">'.$email.'</td>';
$output .='<form name="seleciona" id="seleciona">';
$output .='<td align="left"><select id="ativo" name="ativo">
                        <option value=""></option>
                        <option value="1">Active</option>
                        <option value="2">Pending</option>
                        <option value="3">Suspended</option>
                </select>
    <img class="apply" src="../images/apply.png" width="16" height="16"></td>';

$output .='</form>';
$output .='</tr>';  

然后,当用户单击图像 apply.png 时,我想通过 jQuery 将选择下拉框中的选定选项发送到名为 post_status.php 的 PHP 文件。一切正常,唯一的问题是获取所选框的值并将其与 id 一起发送到 PHP 页面。

这是 jQuery

<script>
$(document).ready(function(){   
  $('#table2 td img.apply').click(function(){
    if (confirm("Do you want to change this status?")) {
      var parent = $(this).closest('TR');
      var id = parent.attr('id');

      var activation ---> here should be the code to catch up the selected value

      $.post("post_activation.php", { id: id, activation: activation } );
      alert("Status has been updated!"+id+activation);
  }
  return false;
});
});
</script>

最后是文件 post_activation.php

<?php
include "../connect_to_mysql.php";

if(isset($_POST['id'])){    
  $id=$_POST['id'];
  $activation=$_POST['activation'];

  $sql=mysql_query("UPDATE agencies 
                    SET 
                    active='$activation', 
                    last_update=CURDATE() 
                    WHERE 
                    id='$id'")or die(mysql_error()); 

}
?>
4

3 回答 3

1

请尝试<select id="ativo'.$id.'" name="ativo">作为 HTML 部分。在 jQuery 部分var activation = $("#ativo"+parent.attr('id')).val();

有你的答案...

于 2013-07-08T15:07:19.107 回答
0

这应该可以找到当前选择的值:

var element = document.getElementById("ativo");
var selected = element.options[element.selectedIndex].text;
activation = selected;

你也可以看看这个演示

于 2013-07-08T14:49:35.487 回答
-1
var activation = $("#ativo").html(); // gives you Active,Pending,Suspended

或者

var activation = $("#ativo").text(); // gives you Active,Pending,Suspended  

var activation = $("#ativo").val(); // gives you 1,2,3
于 2013-07-08T14:55:46.370 回答