收集复选框值jQuery和张贴在提交 [英] Collect checkbox values in jQuery and POST them on submit
问题描述
我已经提到了这个帖子:
I've referred to this post:
<一个href=\"http://stackoverflow.com/questions/1557273/jquery-post-array-of-multiple-checkbox-values-to-php\">jQuery - 多个复选框值的数组后到PHP
和这个jQuery论坛的帖子:
And this jQuery forum post:
的http://forum.jquery.com/topic/checkbox-names-aggregate-as-array-in-a-hidden-input-value
我想收集复选框值的隐藏输入字段使用jQuery数组(或连接字符串,用逗号,等等)。这里的脚本code我使用的是:
I am trying to collect an array (or concatenated string with commas, whatever) of checkbox values in a hidden input field using jQuery. Here's the script code I'm using:
<script type="text/javascript">
$("#advancedSearchForm").submit(function() {
var form = this;
$(form).find("input[name=specialty]").val(function() {
return $("input:checkbox",form).map(function() {
return $(this).attr("name");
}).get().join();
});
});
</script>
有关HTML的一个片段:
A snippet of the relevant HTML:
<form id="advancedSearchForm" name="advancedSearchForm" method="post" action="<?php echo site_url('/magcm/advancedSearch#results'); ?>">
<input type="checkbox" name="FCM" id="FCM" class="chk" value="FCM" <?php echo set_checkbox('FCM', 'FCM'); ?>/>
<input type="hidden" name="specialty" id="specialty" value="" />
<input class="button" name="submit3" id="submit3" type="submit" value="Search" />
我试图改变提交到jQuery的,它打破(显然)submit3。当我的print_r($ _ POST),复选框正确的职位,但冷凝隐变量没有。 (该职位,但空值)的复选框一直存在正确使用CI的砍死SET_VALUE()函数(德里克需要在主干线实现这个......但那是另一回事)
I've tried changing "submit" to "submit3" in the jQuery, which breaks (obviously). When I print_r($_POST), the checkboxes POST correctly but the condensed hidden variable does not. (It posts, but a blank value.) The checkboxes persist correctly using CI's hacked set_value() function (Derek needs to implement this in the main trunk... but that's another story)
我敢肯定,我做的事情是错误的,容易指出。我刚刚撞我的头靠在墙上就它在过去的2个小时,尝试各种功能和改变一吨的东西,在Chrome开发工具对其进行分析(不显示任何错误)。
I'm sure I'm doing something that is wrong and easy to point out. I've just been banging my head against the wall for the past 2 hours on it, trying various functions and changing a ton of things and analyzing it in Chrome dev tools (which don't show any errors).
帮助是AP preciated。 :)
Help is appreciated. :)
推荐答案
我结束了使用PHP数组,而不是jQuery的解决它:
I ended up solving it using PHP arrays rather than jQuery:
<input type="checkbox" name="chk[]" id="RET" class="chk" value="RET" <?php echo set_checkbox('chk', 'RET'); ?>/>
我的名字改为了数组并贴到我的剧本,我在那里通过数组循环和处理它。还不能确定是什么问题是与基于jQuery的解决办法,但我想我会发布此给大家参考的未来。
I changed the name to an array and POSTed it to my script, where I looped through the array and handled it there. Still not sure what the problem was with the jQuery-based solutions, but I figured I'd post this for everyone to refer to in the future.
这篇关于收集复选框值jQuery和张贴在提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!