Javascript 切换按钮打印双值
我的表中有一个切换按钮名为启用、禁用,如果 我将按钮启用的值更改为禁用,然后它将禁用值打印两次 我的代码:Javascript 切换按钮打印双值,javascript,php,jquery,codeigniter,toggle,Javascript,Php,Jquery,Codeigniter,Toggle,我的表中有一个切换按钮名为启用、禁用,如果 我将按钮启用的值更改为禁用,然后它将禁用值打印两次 我的代码: <table id="prodcutTable" class="table table-bordered table-striped" style="width: 100%;" > <thead> <tr style="font-size: 12px; line-height: 0px; ">
<table id="prodcutTable" class="table table-bordered table-striped" style="width: 100%;" >
<thead>
<tr style="font-size: 12px; line-height: 0px; ">
<th>SR No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Status</th>
<th></th>
</tr>
<?php $counter=1?>
<?php foreach($result as $result):?>
<tr>
<input type="hidden" name="txt_user_id" value="<?php echo $result['id']?>">
<td><?php echo $counter?></td>
<td><?php echo $result['username']?></td>
<td><?php echo $result['lastname']?></td>
<td><?php echo $result['email']?></td>
<td><?php echo $result['phonenumber']?></td>
<td>
<?php
if ($result['status']=="active"){
?>
<span class="badge badge-boxed badge-soft-success">Active</span>
<?php
}else if($result['status']=="inactive"){
?>
<span class="badge badge-boxed badge-soft-warning">InActive</span>
<?php
}
?>
</td>
<td>
<input type="checkbox" class="btn-toggle" name="toggle" id="toggle" data-toggle="toggle" data-off="Disabled" data-on="Enabled" <?php
if($result['status']=="active") echo "checked"; ?> >
</td>
</tr>
<?php $counter++?>
<?php endforeach;?>
</thead>
</table>
<script>
function disableUser(id) {
$.ajax({
url: "<?php echo base_url(); ?>admin_controller/AdminController/disableUser",
method: "post",
data: {id:id},
dataType:'json',
})
.done(function( data ) {
if(data.status=="true"){
alert('Enabled');
setTimeout(function(){location.reload();},100);
}else{
alert("Try Again");
}
});
}
function enableUser(id) {
$.ajax({
url: "<?php echo base_url(); ?>admin_controller/AdminController/enableUser",
method: "post",
data: {id:id},
dataType:'json',
})
.done(function( data ) {
if(data.status=="true"){
alert('Disabled');
setTimeout(function(){location.reload();},100);
}else{
alert("Try Again");
}
});
}
$('.btn-toggle').change(function(){
var tr = $(this).parents('tr');
//console.log(tr);
var txt_user_id = tr.find($('input[name="txt_user_id"]')).val();
//console.log(txt_user_id);
var mode= $(this).prop('checked');
console.log(mode);
if($(this).prop('checked'))
{
enableUser(txt_user_id);
}
else
{
disableUser(txt_user_id);
}
});
</script>
高级职员
名字
姓
电子邮件ID
电话号码
地位
活跃的
不活跃的
功能禁用用户(id){
$.ajax({
url:“管理员控制器/AdminController/disableUser”,
方法:“张贴”,
数据:{id:id},
数据类型:'json',
})
.完成(功能(数据){
if(data.status==“true”){
警报(“已启用”);
setTimeout(函数(){location.reload();},100);
}否则{
警惕(“重试”);
}
});
}
功能启用用户(id){
$.ajax({
url:“管理员控制器/AdminController/enableUser”,
方法:“张贴”,
数据:{id:id},
数据类型:'json',
})
.完成(功能(数据){
if(data.status==“true”){
警报(“禁用”);
setTimeout(函数(){location.reload();},100);
}否则{
警惕(“重试”);
}
});
}
$('.btn toggle').change(函数(){
var tr=$(this.parents('tr');
//控制台日志(tr);
var txt_user_id=tr.find($('input[name=“txt_user_id”]).val();
//console.log(txt\u用户\u id);
var mode=$(this.prop('checked');
console.log(模式);
if($(this.prop('checked'))
{
启用用户(txt\U用户\U id);
}
其他的
{
禁用用户(txt\U用户\U id);
}
});
我不知道我在代码中哪里写错了代码。
下面是我的表格的图片,它看起来像一个切换条。
颜色也会从蓝色变为白色
将此代码放入您的
ready
功能:
$('.btn-toggle').bootstrapToggle({
on: 'Enabled',
off: 'Disabled'
});