PHP/jQuery-使用GetOnChange提交表单复选框值
我正在尝试使用PHP/jQuery-使用GetOnChange提交表单复选框值,php,jquery,forms,get,Php,Jquery,Forms,Get,我正在尝试使用GET方法从表单提交复选框值(类别名称)。我使用自定义PHP函数为每个类别名称生成一个键(整数)。我的page.php文件用于我的URLwww.mywebsite.com/shop 在我的页面.php上: <form method="GET" action="" id="sidebar-filter-form"> <ul class="sidebar-filter-options-list"> <?php foreach($ca
GET
方法从表单提交复选框值(类别名称)。我使用自定义PHP函数为每个类别名称生成一个键(整数)。我的page.php
文件用于我的URLwww.mywebsite.com/shop
在我的页面.php上
:
<form method="GET" action="" id="sidebar-filter-form">
<ul class="sidebar-filter-options-list">
<?php foreach($category_list as $key => $row): ?>
<li><input type="checkbox" name="category[]" value="<?php category_key($row->category_name, "k");?>"> <?php echo $row->category_name;?></li>
<?php endforeach; ?>
</ul>
</form>
我正在尝试检索分类键,以便在page.php
上对我的产品进行排序,方法是:
$category_keys = isset($_GET['category']) ? $_GET['category'] : null;
我想知道为什么我的表单没有重新加载我的页面并更改我的URL来检索我的密钥,比如
www.mywebsite.com/shop?category[]=43&category[]=6&category[]=22
(对于Unicode[
和]5B和%5D
)另一个解决方案将是对每个复选框进行设置。试试这个:
<html>
<head>
<title></title>
</head>
<body>
<?
$items = array(
array('id' => 20, 'name' => 'Tomato'),
array('id' => 32, 'name' => 'Milk'),
array('id' => 34, 'name' => 'Sugar')
);
?>
<ul>
<? foreach ( $items as $comboItem ) : ?>
<li><input data-id="<?=$comboItem['id']?>" type="checkbox" class="pick-me"/><?=$comboItem['name']?></li>
<? endforeach; ?>
</ul>
<br>
<button class="get-all">Get all items selected</button>
<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<script type="text/javascript">
(function() {
$('body').on('click','.get-all', function() {
var items = [];
$('.pick-me').each(function() {
if ( $(this).is(':checked') ) {
items.push($(this).attr('data-id'));
}
});
console.log(items);
});
})();
</script>
</body>
</html>
选择所有项目
(功能(){
$('body')。在('click','get all',函数()上{
var项目=[];
$('.pick me')。每个(函数(){
如果($(this).is(':checked')){
items.push($(this.attr('data-id'));
}
});
控制台日志(项目);
});
})();
您确定触发了“更改”事件吗?你确定this.form
选择了这个表单吗?我敢建议你用另一种方式做得更好。为什么不为所有复选框使用一个类,并使用jQuery“each”返回一个包含所选输入的数组?@Roamer-1888,我最终希望在刷新页面之前添加某种延迟(1-2秒),允许用户在提交表单之前选择多个复选框值。我想尽量避免使用提交按钮this.form
可以成功触发更改时的警报消息,因此我相信它确实选择了表单。鉴于#边栏过滤器表单
选择了表单本身,this.submit()
似乎更合适。建议您使用一个提交按钮,然后最终解决自动提交。此外,为了更好的用户体验,考虑让你的应用程序SPA,与非刷新Ajax请求。@ RoAM-1888非常感谢的反馈,<代码>。提交()/代码>的伎俩!
<html>
<head>
<title></title>
</head>
<body>
<?
$items = array(
array('id' => 20, 'name' => 'Tomato'),
array('id' => 32, 'name' => 'Milk'),
array('id' => 34, 'name' => 'Sugar')
);
?>
<ul>
<? foreach ( $items as $comboItem ) : ?>
<li><input data-id="<?=$comboItem['id']?>" type="checkbox" class="pick-me"/><?=$comboItem['name']?></li>
<? endforeach; ?>
</ul>
<br>
<button class="get-all">Get all items selected</button>
<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<script type="text/javascript">
(function() {
$('body').on('click','.get-all', function() {
var items = [];
$('.pick-me').each(function() {
if ( $(this).is(':checked') ) {
items.push($(this).attr('data-id'));
}
});
console.log(items);
});
})();
</script>
</body>
</html>