var add_dynastfileds = function()
{
CheckedFileds = [];
UncheckedFileds = [];
$(".DynFileds").each(function() {
//console.log($(this).val());
if ($(this).is(":checked")) {
if(CheckedFileds.indexOf($(this).val())==-1){
CheckedFileds.push($(this).val());
}
} else {
if(UncheckedFileds.indexOf($(this).val())==-1){
UncheckedFileds.push($(this).val());
}
}
});
$.post("<?=base_url()?>asset/addDynFields/",{CheckedFileds:CheckedFileds,UncheckedFileds:UncheckedFileds},function(result)
{
//console.log(result);
$.gritter.add({
title: "Fields updated successfully ",
text: "",
time: 2000
});
});
}
<div class="panel-group">
<div class="panel panel-default">
<div class="row">
<?
foreach($dynast_fields as $dynfileds){
if(($dynfileds['display_name'] == 'Title') &&
($dynfileds['add_to_listing'] =='1'))
{
$styleClass = 'disabled="disabled"';
$checked = "checked";
}else{
$styleClass = '';
$checked = "";
}
?>
<div class="col-md-1"></div>
<input type="checkbox" <?=$styleClass?> <?=$styleClass?> class="DynFileds" name="dynfileds<?=$dynfileds['rec_no']?>" id="dynfileds<?=$dynfileds['rec_no']?>" value="<?=$dynfileds['rec_no']?>" <? if($dynfileds['add_to_listing'] == '1') { echo "checked"; } ?>/>
<span style="font-size:12px;font-family: Open Sans,Helvetica Neue,Helvetica,Arial,sans-serif;"><?=$dynfileds['display_name']?></span>
<br/>
<?php
}
?>
</div>
</div>
<span id='dup_checkdiv' style="display:none;" class="text-danger"></span>
<br/>
<div class="popover-footer" style="align:center;">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-3">
<a href="javascript:;" style="font-family: Open Sans,Helvetica Neue,Helvetica,Arial,sans-serif;" class="btn btn-info btn-sm" onclick="add_dynastfileds()">Save</a>
</div>
</div>
</div>
</div>
public function addDynFields()
{
$checkedfileds = $_POST['CheckedFileds'];
$uncheckedFileds = $_POST['UncheckedFileds'];
$this->db->update('pm1asset_dynamic_fields',array('add_to_listing' =>0));
if(!empty($checkedfileds))
{
for($i = 0; $i < count($checkedfileds); $i++)
{
$data = array(
'add_to_listing' =>1
);
$this->db->set($data);
$this->db->where('rec_no',$checkedfileds[$i]);
$this->db->update('pm1asset_dynamic_fields');
}
}
}
在这里,我写了一些代码来更新checked和unchecked复选框的值,这是第一次通过将值"0"上传到unchecked和将值"1"上传到checked复选框来正常工作,但在重新加载页面后,如果我要通过取消选中上一个复选框来更新值"0,它不会更新,因为页面刷新后,复选框值仍处于选中状态。出现这个问题是因为我在div标记的复选框中给出了条件,但我也需要这个条件。有什么不同的写法吗。谁能告诉我如何解决这个问题吗?
试试这样的
$('.DynFileds:checked').each(function() {
CheckedFileds.push(this.value);
});
$('.DynFileds:not(:checked)').each(function() {
UncheckedFileds.push(this.value);
});
更多
- 在jquery中查找所有未选中的复选框
- 使用jQuery获取所选复选框的值