我有一个显示名称的表,但其值是userId,email和level
<?php if (count($users)): ?>
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>E-mail</th>
<th>Level</th>
<th></th>
</tr>
</thead>
<?php foreach ($users as $user): ?>
<tr>
<td><a href="<?php echo $this->url('user/edit', array('id' => $user->id)); ?>"><?php echo $this->escapeHtml($user->first_name . ' ' . $user->last_name); ?></a></td>
<td><?php echo $this->escapeHtml($user->email); ?></td>
<td>
<select name="level" id= "level" onchange="changeLevel(this);">
<?php $level = $this->escapeHtml($user->level); ?>
<?php if ($level == 1): ?>
<option value="1" selected ="selected">Administrator</option>
<option value="2" <?php IsSelected($level,2); ?>> Manager</option>
<option value="3" <?php IsSelected($level,3); ?>>HR Staff</option>
<?php elseif ($level == 2): ?>
<option value="1" <?php IsSelected($level,1); ?>>Administrator</option>
<option value="2" selected ="selected">Manager</option>
<option value="3" <?php IsSelected($level,3); ?>>HR Staff</option>
<?php elseif ($level == 3): ?>
<option value="1" <?php IsSelected($level,1); ?>>Administrator</option>
<option value="2" <?php IsSelected($level,2); ?>>Manager</option>
<option value="3" selected ="selected">HR Staff</option>
<?php endif; ?>
</select>
</td>
<td style="text-align: right;">
<a href="<?php echo $this->url('user/delete', array('id' => $user->id)); ?>" class="btn btn-mini btn-danger" rel="tooltip" title="Delete this user"><i class="icon-remove icon-white"></i></a>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php else: ?>
<h3>There are no registered users available.</h3>
<?php endif; ?>
现在,我的select dropdown onchange事件只将下拉列表中所选值的值传递给调用zend controlelr的ajax函数
这里是ajax函数
<script>
changeLevel = function(level){
var level_value = level.value;
var user_id =
alert(level.value);
$.ajax({
type: "POST",
url: 'http://localhost/ijm/public/user/level',
data: { function_params: level_value },
Success: function(result){
alert('Success');
}
});
}
</script>
这是用户控制器功能
public function levelAction()
{
$storage = $this->getAuthService()->getStorage();
$user = $storage->read();
$request = $this->getRequest();
$level = $request->getPost('function_params');
echo $level;
// Check if form values are valid
$this->getUserTable()->editLevel($user->id, $level);
$this->redirect()->toRoute('user');
echo "success";
}
我想要的是每当我触发onchange事件时我也希望userId传递onchange事件功能,以便我可以在我的zend控制器上发送它。我怎样才能做到这一点?提前谢谢。
答案 0 :(得分:4)
<select name="level" id= "level" onchange="changeLevel(this , '<?php echo $user->id?>');">
...
changeLevel = function(level, user_id){
...
答案 1 :(得分:1)
您的select标签也可以包含用户ID:
<select name="level" id="level" data-userid="<?=$user->id?>" onchange="changeLevel(this);">
与changeLevel
功能相比,您可以通过以下方式访问它:
changeLevel = function(level){
var level_value = level.value;
var userid = $(level).data('userid');
alert('Value: ' + level.value + ', userid: ' + userid);
$.ajax({
type: "POST",
url: 'http://localhost/ijm/public/user/level',
data: { function_params: level_value, userid: userid },
Success: function(result){
alert('Success');
}
});
}
请检查this Fiddle。