[英]How to get the value of selected checkbox in jquery and assign it to a json array?
我有一个问题,我对 jquery 知之甚少,这就是为什么我很难处理我的代码。 我有一个产品分页。 上面有复选框。 我的简单目标是在选中复选框时禁用产品。 但是我不能以一种形式执行此操作,因为我的表单中已经有另一个过程,即如果选中则删除产品。 这就是为什么我计划在我的 jquery 中创建一个表单操作并将所有产品 ID 从我的复选框传递到一个 json 数组。 但是我该怎么做呢?
这是我的一些代码
<form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form">
<table class="list">
<thead>
<tr>
<td width="1" style="text-align: center;"><input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" /></td>
<td class="left"><?php echo $column_name; ?></td>
<td class="right"><?php echo $column_sort_order; ?></td>
<td class="left"><?php echo $column_status; ?></td>
<td class="right"><?php echo $column_action; ?></td>
</tr>
</thead>
<tbody>
<?php if ($categories) { ?>
<?php foreach ($categories as $category) { ?>
<tr>
<td style="text-align: center;"><?php if ($category['selected']) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $category['category_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $category['category_id']; ?>" />
<?php } ?></td>
<td class="left"><?php echo $category['name']; ?></td>
<td class="right"><?php echo $category['sort_order']; ?></td>
<td class="right"><?php echo ($category['status'] == 1 ? 'Enable' : 'Disable'); ?></td>
<td class="right"><?php foreach ($category['action'] as $action) { ?>
[ <a href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a> ]
<?php } ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="center" colspan="4"><?php echo $text_no_results; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
<br />
<div align="right">
<select name="action_select">
<option value="enable">Enable Selected</option>
<option value="disable">Disable Selected</option>
</select>
<input type="button" class="button" id="update_status" value="Update Status" />
</div>
<br />
这是我的 jquery 示例
<script type="text/javascript">
$("#update_status").on('click', function(){
//how can I get the id of my checkboxes and assign it to a json array
//How can I create a form inside it?
});
</script>
这就是所有我希望你能理解我的问题。 谢谢。
否则你可以使用下面的示例代码
<script>
$(document).ready(function()
{
$("input[type=checkbox]").click(function()
{
var categoryVals = [];
categoryVals.push('');
$('#Category_category :checked').each(function() {
categoryVals.push($(this).val());
});
$.ajax({
type:"POST",
url:"<?php echo $this->createUrl('ads/searchresult'); ?>", //url of the action page
data:{'category': categoryVals},
success : function(response){
//code to do somethng if its success
}
});
}
}
</script>
尝试
$('input[name="selected[]"]:checked').each(function() {
console.log(this.value);
});
有关更多信息:- 使用 jQuery 获取选定复选框的值
尝试
var values = $('.list input[name="selected[]"]:checked').map(function () {
return this.value;
}).get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.