【发布时间】:2017-07-08 11:51:13
【问题描述】:
我想在单击复选框时重写 url 参数值,类似于 LinkedIn Advance 搜索。
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script type='text/javascript'>
$(document).ready(function () {
$('input[type=checkbox]').click(function (e) {
var seasoning = jQuery.map($(':checkbox[id=seasoning\\[\\]]:checked'), function (n, i) {return n.value;}).join(',');
window.location ='example.com?seasoning='+seasoning;
});
});
</script>
<h3>Filter recepies:</h3>
<div>
<p>Select vegetables</p>
<label><input type="checkbox" id="vegetables[]" value="potato"> Potato</label><br>
<label><input type="checkbox" id="vegetables[]" value="onion"> Onion</label><br>
<label><input type="checkbox" id="vegetables[]" value="tomato"> Tomato</label><br>
</div>
<div>
<p>Select seasoning</p>
<label><input type="checkbox" id="seasoning[]" value="salt"> Salt</label><br>
<label><input type="checkbox" id="seasoning[]" value="pepper"> Pepper</label><br>
<label><input type="checkbox" id="seasoning[]" value="chilli"> Chilli Flakes</label><br>
</div>
我想要的结果
Click1: 当我从蔬菜中点击 Potato 时,我的 URL 应该如下所示
example.com?vegitables=potato
Click2: 当我从蔬菜中点击 Onion 时,我的 URL 应该如下所示
example.com?vegitables=potato,onion
Click3: 当我点击调味盐时,我的 URL 应该看起来像
example.com?vegitables=potato,onion&seasoning=salt
Click4:当我点击辣椒调味时,我的网址应该是这样的
example.com?vegitables=potato,onion&seasoning=salt,pepper
【问题讨论】:
-
检查我发布的答案,正是你需要的。
标签: javascript jquery url post