我正在这个项目中工作,我面临这个问题。我的代码在加载后单击时有效。我为每个用户使用了 id。这是活动/非活动用户的代码。我将 id 传递给每个文本框和跨度。它不工作。就如何解决此问题提供一些想法。PHP代码:
<!--top section start-->
<?php $this->load->view("header");?>
<div id="wrapper" style="height: auto;">
<div class="user_intro">
<table width="600" height="300">
<th style="text-align:left;">First Name</th>
<th style="text-align:left;">Email</th>
<th style="text-align:center;">Active Status </th>
<?php
foreach($result as $user)
{?>
<tr>
<td style="text-align:left;"><a href="<?php echo base_url()?>siteadmin/home/userdetail?userid=<?php echo $user->user_id; ?>"><?php echo $user->first_name;?></a></td>
<td style="text-align:left;"><a href="<?php echo base_url()?>siteadmin/home/userdetail?userid=<?php echo $user->user_id; ?>"><?php echo $user->email;?></a></td>
<?php if ($user->status == 1) { ?>
<td style="text-align:center;"><input type="button" id="<?php echo $user->user_id; ?>" value="" class="on" onclick="togglestyle(this,this.id)" /> </td>
<td><span id="reason_<?php echo $user->user_id; ?>"><input type="text" value="<?php echo $user->reason; ?>" /> </span></td>
<?php }
else {?>
<td style="text-align:center;"><input type="button" id="<?php echo $user->user_id; ?>" value="" class="off" onclick="togglestyle(this,this.id)" /> </td>
<td><span id="reason_<?php echo $user->user_id; ?>"><input type="text" value="<?php echo $user->reason; ?>" /> </span></td>
<?php } ?>
</tr>
<?php }
?>
</table>
</div>
</div>
<!--fotter section start-->
</div> <?php $this->load->view("footer"); ?>
我在头文件中添加的javascript代码:
<script type="text/javascript">
function togglestyle(el, id) {
if (el.className == "on") {
$("#reason_" + id).show();
$.ajax({
type: "POST",
url: "statuson",
data: "userid=" + id,
success: function (html) {
if (html == 'true') {
el.className = "off";
}
},
});
} else {
$("#reason_" + id).hide();
$.ajax({
type: "POST",
url: "statusoff",
data: "userid=" + id,
success: function (html) {
if (html == 'true') {
el.className = "on";
}
},
});
}
}
</script>