Php 如何从输入创建数组并使用jQuery在url中发送它?
我有一个过滤器的问题,它排序一页Php 如何从输入创建数组并使用jQuery在url中发送它?,php,jquery,Php,Jquery,我有一个过滤器的问题,它排序一页 <?php $vendors = array(1 => 'vendor1', 2 => 'vendor2', 3 => 'vendor3'); $vendorChecked = array(1,3); ?> <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-la
<?php
$vendors = array(1 => 'vendor1', 2 => 'vendor2', 3 => 'vendor3');
$vendorChecked = array(1,3);
?>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$( "#vendorForm" ).submit(function( event ) {
var url = 'index.php?show=';
var vendors = $('#vendors').map(function(){return $(this).val();}).get();
var redirect = url+encodeURIComponent(vendors);
console.log(redirect);
//window.location.href = url+encodeURIComponent(vendors);
});
});
</script>
</head>
<body>
<div class="sub2">
<form class="form-inline" id="vendorForm" method="post" style="margin-bottom: 0px; width: 80%">
<? foreach ($vendors as $k => $v): ?>
<label style="padding-right: 10px;" class="checkbox inline"><input <?= in_array($k, $vendorChecked)?'checked':'' ?> type="checkbox" name="vendors[]" id="vendors" value="<?=$k?>"> <?=$v?></label>
<? endforeach; ?>
<button class="btn" type="submit"> Show</button>
</form>
</div>
</body>
</html>
$(文档).ready(函数(){
$(“#供应商表单”)。提交(功能(事件){
var url='index.php?show=';
var vendors=$('#vendors').map(函数(){return$(this.val();}).get();
var redirect=url+encodeURIComponent(供应商);
控制台日志(重定向);
//window.location.href=url+encodeURIComponent(供应商);
});
});
显示
我想在提交表单后重定向,url应该是:
index.php?show=1:2:3
1:2:3为选中字段尝试
$("#vendorForm").submit(function (event) {
var url = 'index.php?show=';
//You cannot not use id as ID must the unique, in thise case use the name attribute
//Also use :checked to filter only checked checkboxes
var vendors = $('input[name="vendors[]"]:checked').map(function () {
return this.value;
}).get();
//use .join() to concatenate the values
var redirect = url + encodeURIComponent(vendors.join(':'));
console.log(redirect);
//window.location.href = url+encodeURIComponent(vendors);
});
演示: