我有这个jquery, 然后我用foreach来显示我数据库中的所有记录。
现在我试图做的是将值传递给jquery,但该值只读取了第一个td。
我的jquery
jQuery('#nannyedit').click(function(){
jQuery('<img src="<?php echo plugins_url( 'images/jon-loader.gif', __FILE__ ); ?>" id="loading" />').appendTo("#container");
var unanny = jQuery(this).parent().siblings("input[name=nanny]:first").val();
var access = jQuery( 'input[name=access]' ).val();
alert( unanny );
jQuery.ajax({
type: "GET",
url: "<?php echo plugins_url( 'edit.php', __FILE__ ); ?>",
data: { user_login: unanny, access: access },
dataType: 'html',
target: '#tab-1',
success: function(data){
jQuery("#container").find('img#loading').remove();
jQuery("#container").html(data);
}
})
});
我的HTML
<table cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Status</th>
<th>Photo</th>
<th>Resume</th>
<th>Date</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php
global $wpdb;
$mynanny = $wpdb->get_results( "SELECT * FROM jon_nanny" );
$color1 = "#FDF7E1";
$color2 = "#FFFFFF";
$row_count = 0;
foreach ($mynanny as $nanny) {
$mynannyphoto = $wpdb->get_row( "SELECT * FROM jon_nanny_photo WHERE user_login = '".$nanny->user_login."' " );
$mynannyresume = $wpdb->get_row( "SELECT * FROM jon_nanny_resume WHERE user_login = '".$nanny->user_login."' " );
$row_color = ($row_count % 2) ? $color1 : $color2;
$uploads = wp_upload_dir();
$upload_dir = ( $uploads['url'] );
?>
<tr>
<td style="background-color:<? echo $row_color; ?>"><?php echo $nanny->user_login; ?></td>
<td style="background-color:<? echo $row_color; ?>"><?php echo $nanny->email; ?></td>
<td style="background-color:<? echo $row_color; ?>"><?php echo $nanny->fname; ?></td>
<td style="background-color:<? echo $row_color; ?>"><?php echo $nanny->mname; ?></td>
<td style="background-color:<? echo $row_color; ?>"><?php echo $nanny->lname; ?></td>
<td style="background-color:<? echo $row_color; ?>"><?php echo $nanny->user_status; ?></td>
<td style="background-color:<? echo $row_color; ?>">
<img src="<?php echo $upload_dir.'/'.$mynannyphoto->imgname; ?>" width="150" />
</td>
<td style="background-color:<? echo $row_color; ?>">
<a href="<?php echo $upload_dir.'/'.$mynannyresume->resume; ?>" />
<img src="<?php echo plugins_url( 'images/resume.png', __FILE__ ); ?>" />
</a>
</td>
<td style="background-color:<? echo $row_color; ?>"><?php echo $nanny->date; ?></td>
<td style="background-color:<? echo $row_color; ?>" id="getnan">
<?php
$url = admin_url();
?>
<input type="hidden" name="nanny" value="<?php echo $nanny->user_login;?>" />
<input type="hidden" name="access" value="nanny" />
<a href="#" id="nannyedit">
<img src="<?php echo plugins_url( 'images/edit-file-icon.png', __FILE__ ); ?>" />
</a>
<a href="">
<img src="<?php echo plugins_url( 'images/delete-file-icon.png', __FILE__ ); ?>" />
</a>
<a href="" title="View">
<img src="<?php echo plugins_url( 'images/Document-icon.png', __FILE__ ); ?>" />
</a>
</td>
</tr>
<?php
$row_count++;
}
?>
</tbody>
</table>
我的jquery错了吗? 请告诉我谢谢。
答案 0 :(得分:0)
将foreach循环更改为while
while($mynanny = $wpdb->get_results( "SELECT * FROM jon_nanny" )){
答案 1 :(得分:0)
问题是你在foreach循环中使用#nannyedit。
id属性指定HTML元素的唯一ID(该值在HTML文档中必须是唯一的。)
更改您的代码:
脚本中的:
jQuery('.nannyedit').click(....
在foreach中:
<a class='nannyedit'> ...