[英]Pass checkbox values with Jquery to PHP and display result in div
I want to filter realtime results with jQuery (just like on this site http://shop.www.hi.nl/hi/mcsmambo.p?M5NextUrl=RSRCH ). 我想使用jQuery过滤实时结果(就像在此网站上http://shop.www.hi.nl/hi/mcsmambo.p?M5NextUrl=RSRCH一样 )。 So when someones checks a checkbox the results should update realtime (in a div).
因此,当某人选中一个复选框时,结果应实时更新(以div为单位)。 Now I'm a newbie with jQuery and I've tried lots of examples but I can't get it to work.
现在,我是jQuery的新手,我尝试了很多示例,但无法正常工作。 Here's my code, could anyone tell what I'm doing wrong?
这是我的代码,有人可以告诉我我做错了吗? Thank you very much!
非常感谢你!
HTML HTML
<div id="c_b">
Kleur:<br />
<input type="checkbox" name="kleur[1]" value="Blauw"> Blauw <br />
<input type="checkbox" name="kleur[2]" value="Wit"> Wit <br />
<input type="checkbox" name="kleur[3]" value="Zwart"> Zwart <br />
<br />
Operating System:<br />
<input type="checkbox" name="os[1]" value="Android"> Android <br />
<input type="checkbox" name="os[2]" value="Apple iOS"> Apple iOS <br />
</div>
<div id="myResponse">Here should be the result</div>
jQuery jQuery的
function updateTextArea() {
var allVals = [];
$('#c_b :checked').each(function() {
allVals.push($(this).val());
});
var dataString = $(allVals).serialize();
$.ajax({
type:'POST',
url:'/wp-content/themes/u-design/filteropties.php',
data: dataString,
success: function(data){
$('#myResponse').html(data);
}
});
}
$(document).ready(function() {
$('#c_b input').click(updateTextArea);
updateTextArea();
});
PHP PHP
//Just to see if the var passing works
echo var_export($_POST);
You are using .serialize() incorrectly, it works only with form elements. 您使用的.serialize()错误,它仅适用于表单元素。
With this code i think you'll get what you need. 有了这段代码,我想您会得到您所需要的。
Javascript / JQuery Javascript / JQuery
function updateTextArea() {
var allVals = "";
$('#c_b input[type=checkbox]:checked').each(function() {
currentName = $(this).attr("name");
currentVal = $(this).val();
allVals = allVals.concat( (allVals == "") ? currentName + "=" + currentVal : "&" + currentName + "=" + currentVal );
});
$.ajax({
type:'POST',
url:'/wp-content/themes/u-design/filteropties.php',
data: allVals,
dataType: "html",
success: function(data){
$('#myResponse').html(data);
}
});
}
$(document).ready(function() {
$('#c_b input[type=checkbox]').click(updateTextArea);
updateTextArea();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.