javascript - 如何使用jquery从不同的类中获取值
问题描述
如何使用 jquery 从不同的类中获取值。我点击编辑和删除,我想获取 idUser 值和用户名并使用 jquery 进行调整,然后可以将其传递给控制器,但无法检索值并且 onclick 无法像重定向到下一页一样运行进行编辑,没有要删除的操作。如何从不同的类中获取值并且 onclick 可以工作?求各位帮忙,如有不足请见谅,如果有不清楚的地方可以问我...
注意:我使用 php 原生 MVC
这是我查看表 user_management.php 的代码
<?php
$main_controll = new App_UserManagement_Control_UserManagement();
$data_from_ctr = $main_controll->usermanagement();
?>
<script type="text/javascript">
$(document).ready(function(){
$('.delete').click(function(event){
event.preventDefault();
if(confirm("Are you sure want to delete ?")){
var username = $(this).attr("username");
var url = $(this).attr("href");
$.ajax({
type:'post',
url: url,
data:{username:username},
success: function(data){
alert("Data Berhasil dihapus")
location.href = '<?php echo BASE_URL. "app.php/usermanagement/user_management "?>';
},
error:function(err){
alert("Data Gagal Dihapus")
}
});
}
});
$('.edit').click(function(event){
event.preventDefault();
var iduser =$(this).attr("iduser");
var url =$(this).attr("href");
console.log(iduser);
//console.log(url);
$.ajax({
type:'post',
url: url,
data:{iduser:iduser},
success:function(data){
alert(data)
location.href = '<?php echo BASE_URL. "app.php/usermanagement/edit_user "?>';
},
error:function(err){
alert(err)
}
});
});
});
</script>
<fieldset>
<legend> User Management </legend>
<br />
<span style="font-size: 15px;">
<a style="text-decoration: underline" href="<?php echo BASE_URL. "app.php/usermanagement/add_user"?>">Add User</a>
</span>
<br /><br />
<table cellspacing="2" cellpadding="2" border="0" align="left" id="tablecontent">
<thead style="background-color:#eee;">
<th width="25">#</th>
<!--<th width="25">id</th>-->
<th width="80">Username</th>
<th width="117">Name</th>
<th width="117">Company Code</th>
<th width="117">Company Name</th>
<th width="80">User Access</th>
<th width="80">Login Status</th>
<th width="80">User Status</th>
<th width="200">Action</th>
</thead>
<tbody>
<?php if($data_from_ctr['user'] !== NULL): ?>
<?php $i = 1; foreach ($data_from_ctr['user'] as $row):
?>
<!--?php
$No=0;
foreach($data_from_ctr['user'] as $row) {
$No++;
?>-->
<tr>
<td><?php echo $i++; ?></td>
<!--<td><?php //echo $row ['idx'] ?></td>-->
<td><?php echo $row ['_user'] ?></td>
<td><?php echo $row ['_fullName'] ?></td>
<td><?php echo $row ['_pyrCode'] ?></td>
<td><?php echo $row ['_desc'] ?></td>
<td><?php echo $row ['group_user'] ?></td>
<!--<td><?php //echo $row ['_flag'] ?></td>-->
<td>
<?php
$status = $row['_flag'];
if($status == 0){
echo "Offline";
}elseif ($status == "") {
echo "Online";
}else{
echo "Online";
}
?>
</td>
<td>
<?php
$active = $row ['active'];
if($active == 1){
echo "Active";
}else{
echo "Non Active";
}
?>
</td>
<td>
<a class="edit" href="<?php echo BASE_URL. "app.php/usermanagement/edit "?>" iduser="<?php echo $row ['idx'] ?>">Edit</a>
<a class="delete" href="<?php echo BASE_URL. "app.php/usermanagement/delete "?>" username="<?php echo $row ['_user'] ?>">Delete</a>
<a onclick="///return confirm('Are you sure want reset {{ row._fullName }} password ?')" href="#">Reset Password</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</fieldset>
这是我的控制器 usermanagement.php 功能编辑
public function edit(){
Template::setTitle('Edit User Management');
$iduser =(int)Request::post('iduser');
//$idUser = (int)Session::get('idx');
$result = $this->getUserbyId($iduser);
$dataresult = json_decode($result, true);
if($dataresult === NULL) {
echo "<script language = \"javascript\">window.alert(\"\Don't Have Data\");";
echo "javascript:history.back();</script>";
return false;
}
$data = $dataresult;
return $data;
}
这是我来自控制器 usermanagement.php 删除功能的代码
public function delete(){
$username = Request::post('username');
echo json_encode($username);
if( !empty($username)){
$url="http://localhost:8585/delete-user/$username";
//echo $url; die;
//$link_url = urlencode($url);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
//Route::redirect(BASE_URL.'app.php/usermanagement/user_management');
return $result;
}
}
这是我的控制器 usermanagement.php 函数 getUserbyId
public function getUserbyId($iduser){
echo json_encode($iduser);
if(!empty($iduser)){
$url="http://localhost:8585/get-user/$iduser";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPGET, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
if($result === FALSE){
die('Curl failed: ' .curl_error($curl));
}
curl_close($curl);
return $result;
}
这是我的页面 edit_user.php
<?php
$main_controll = new App_UserManagement_Control_UserManagement();
$data_from_ctr = $main_controll->edit();
?>
<script type="text/javascript">
$(document).ready(function(){
$('#level').change(function(){
var val =$(this).val();
if(val == '03' || val == '04'){
$('.limit').show();
}else{
$('.limit').hide();
}
});
$('#access').change(function(){
var val = $(this).val();
if(val == 'user'){
$('.level').show();
}else{
$('.level').hide();
$('.limit').hide();
}
});
$('#acctAccessed').chosen();
$("#chkall").click(function(){
if($("#chkall").is(':checked')){
$(".chosen-select option").prop('selected', true);
}else{
$(".chosen-select option").prop('selected', false);
}
$(".chosen-select").trigger("chosen:updated");
});
$(".chosen-select").chosen();
});
</script>
<form method="POST">
<fieldset>
<legend> Edit User Management </legend>
<table border="0" width="700" id="usermanagement">
<tbody>
<tr>
<td width="160"><strong>Company</strong></td>
<td width="10">:</td>
<td width="193" colspan="2" class="company"><span class ="id_company"><?php echo Session::get('pyrCode'); ?></span> <?php echo $data_from_ctr['account']['desc']['desc'];?></td>
</tr>
<tr class="odd">
<td width="150"><strong>Username</strong></td>
<td width="10">:</td>
<td colspan="2">
<input type="text" name="username" id="username" value="<?php echo $data_from_ctr['user']['_user']; ?>" /></td>
</tr>
<tr>
<td width="150"><strong>Email</strong></td>
<td width="10">:</td>
<td colspan="2">
<input type="text" name="email" id="email" value="<?php echo $data_from_ctr['user']['email']; ?>" />
</td>
</tr>
<tr class="odd">
<td width="150"><strong>Status User</strong></td>
<td width="10">:</td>
<td colspan="2">
<input type="radio" name="status_active" value="<?php echo $data_from_ctr['user']['active']; ?>" checked="" id="aktif"/>
Active
<input type="radio" name="status_active" value="<?php echo $data_from_ctr['user']['active'];?>" id="nonaktif" />
Non Active
</td>
</tr>
<tr>
<td width="150"><strong>Name</strong></td>
<td width="10">:</td>
<td colspan="2">
<input type="text" name="name" id="name" value="<?php echo $data_from_ctr['user']['_fullName']; ?>" />
</td>
</tr>
<tr class="odd">
<td width="150"><strong>Phone Number</strong></td>
<td width="10">:</td>
<td colspan="2">
<input type="text" name="phone" id="phone" value="<?php echo $data_from_ctr['user']['_noHP']; ?>" />
</td>
</tr>
<tr>
<td width="150"><strong>Access Control</strong></td>
<td width="10">:</td>
<td colspan="2">
<select id="access" name="access">
<option value="user">User</option>
<option value="admin_bank">Admin Bank</option>
<option value="admin_client">Admin Client</option>
</select>
</td>
</tr>
<tr class="odd level">
<td width="150"><strong>Level</strong></td>
<td width="10">:</td>
<td colspan="2">
<select id="level" name="level">
<option value="01">Inputer</option>
<option value="02">Verificator</option>
<option value="03">Authorize 1</option>
<option value="04">Authorize 2</option>
<option value="05">COPS</option>
</select>
</td>
</tr>
<tr style="display: none" class="odd limit">
<td width="150"><strong>Limit Transaction</strong></td>
<td width="10">:</td>
<td colspan="2">
<input type="text" name="limit" value="<?php echo $data_from_ctr['user']['limit']; ?>" />
</td>
</tr>
<tr>
<td width="150"><strong>Account Access</strong></td>
<td width="10">:</td>
<td colspan="2">
<select id="acctAccessed" name="acctAccessed[]" multiple="multiple" class="chosen-select" style="width:350px;" data-placeholder="Select account">
<?php foreach ($data_from_ctr['account']['account'] as $data) :?>
<option value="<?php echo $data['id'] ?>"><?php echo $data['_giro_OB']; ?></option>
<?php endforeach; ?>
</select>
<input id="chkall" type="checkbox" >Select All</input>
</td>
</tr>
<tr class="odd">
<td width="150"> </td>
<td width="10"> </td>
<td colspan="2"><input class="btnsubmitdis" type="submit" value="Save" /></td>
</tr>
</tbody>
</table>
</fieldset>
这是我的代码edit.php
<?php
$ctr = new App_UserManagement_Control_UserManagement();
$ctr ->edit();
?>
这是我的问题:我有一个数据 idUser 在此处输入图像描述
但是当我单击确定时,我想重定向页面 edit_user.php 但 在此处显示此输入图像描述
我想如果我点击编辑显示此页面 在此处输入图像描述
解决方案
我认为这个例子会帮助你
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
</head>
<body>
<a class="edit" idUser="1">Edit</a>
<a class="delete" username="Samrat">Delete</a>
<script>
$('.delete').click(function (event) {
event.preventDefault();
var username = $(".delete").attr("username");
var useId = $(".edit").attr("idUser");
alert("UserID : " + useId + " And UserName :" + username);
});
</script>
</body>
</html>
推荐阅读
- jhipster - JHipster - 在 compileJava 任务期间“文件管理器问题:尝试为类型重新创建文件”
- node.js - 每次我使用 OAuth 2 和 Passportjs 登录测试 Google 帐户时都会收到一个新的访问令牌和刷新令牌
- laravel - 如何根据 Laravel 中的嵌套值(required_if)进行验证
- java - Lombok 生成的构造函数不能在 SpringBootTest 中隐式自动装配
- java - MySql(休眠)查询
- mqtt - 为什么不能将主题别名存储在 MQTT v5 中的持久会话中
- shell - 获取 + if [[ -z "$CI_COMMIT_TAG" ]]; 然后 + ~ ... 使用 shell 执行程序时 Windows 10 上的错误
- reactjs - 导出具有 Material-UI 主题支持的辅助函数
- c# - 如何使用不允许设置空列表的列表属性反序列化对象的 XML?C#
- visual-studio-code - 如何在 vscode c/c++ intelisense 中禁用错误弹出消息