我需要更新数据库中的“ display_name”,为此,我使用wordpress函数和foreach显示所有用户,并在每个用户旁边添加一个文本框(以更改名称)和一个按钮(以执行操作),我的问题是,如果我正在循环用户,我如何才能获得我所单击的输入和按钮的正确ID?
<?php global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM $wpdb->users" );
?>
<form>
<?php
foreach ($results as $key ) {
echo "$key->ID . $key->display_name <input type='text' name=''> <button>Submit</button>
}
?>
</form>
我目前被困在其中,我得到了ID,但第一个文本框的值
<?php
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM $wpdb->users" );
?><form id="form1"><?php
foreach ($results as $key ) {
echo "$key->ID . $key->display_name . $key->mg_nobility <input type='text'
id='edit_value' class='$key->ID' value=''> <button row_id='$key-
>ID'class='edit_nov'>Submit</button>";
}
?>
</form>
<input type='hidden' id='row_del_id' value=''>
<input type='hidden' id='row_up_id' value=''>
<script>
jQuery('.edit_nov').click(function()
{
var current_id = jQuery(this).attr('row_id');
var current_value =jQuery('#edit_value').val();
jQuery('#row_del_id').val(current_id);
jQuery('#row_up_id').val(current_value);
return false;
});
</script>
答案 0 :(得分:0)
您必须具有唯一的ID值,否则只会得到第一个ID,请尝试以下操作:
<?php
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM $wpdb->users" )
?>
<form id="form1">
<?php foreach($results as $key): ?>
<?php echo $key->ID.$key->display_name.$key->mg_nobility ?>
<!-- Append the id to the end of this -->
<input type='text' id='edit-value-<?php echo $key->ID ?>' value='' />
<!-- Use the data attr instead -->
<button data-rowid='<?php echo $key->ID ?>' class='edit_nov'>Submit</button>
<?php endforeach ?>
</form>
<!-- I don't understand the function of these since they are outside the form -->
<input type='hidden' id='row_del_id' value=''>
<input type='hidden' id='row_up_id' value=''>
<script>
jQuery(function($) {
$(this).on('click', '.edit_nov', function() {
// I would use the native method of .data()
var current_id = $(this).data('rowid');
// Append id and get value
var current_value = $('#edit-value-'+current_id).val();
$('#row_del_id').val(current_id);
$('#row_up_id').val(current_value);
});
});
</script>