这是我的表单&我当前的URL是…/pg/members/all
<form id="simplesearch" name="simplesearch" action="<?php echo $vars['url'];?>pg/members/searchuser" method="post">
<table class="people_search" style="border:none;">
<div id="toggle_profile_type">
<input type="hidden" value="0" name="meta_data_array_search_criteria[custom_profile_type]">
<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39242" class="messageCheckbox"><span>Job Seeker</span></p>
<p class="skills_odd1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39243" class="messageCheckbox"><span>Employer</span></p>
<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39449" class="messageCheckbox"><span>college</span></p>
</div>
</table>
</form>
在脚本中我写了
$(document).ready(function(){
$(":checkbox").click(function(){
simplesearchsubmitform();
});
});
function simplesearchsubmitform()
{
if(document.simplesearchonsubmit && !document.simplesearch.onsubmit()) {
return;
}
document.simplesearch.submit();
}
我正在提交表单,然后URL更改..../pg/members/searchuser。现在我需要得到选定的checkbox
值。这样我就可以把这个值附加到div &此复选框将保持选中状态。
你们知道怎么做吗?我找了很多,但没有找到解决办法。
At "pg/members/searchuser"
更新2.0您可以使用以下代码:
<?php
if(isset($_POST['meta_data_array_search_criteria']))
{
foreach($_POST['meta_data_array_search_criteria'] as $val)
{
foreach($val as $checkbox_data)
{
echo $checkbox_data."<br/>";
}
}
} else
{
echo "No checkbox checked";
}
?>
如何选择复选框:有两种方法,用JS或用PHP。jQuery代码片段:
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
<?php
if(isset($_POST['meta_data_array_search_criteria']))
{
foreach($_POST['meta_data_array_search_criteria'] as $val)
{
foreach($val as $checkbox_data)
{
?>
$('input:checkbox[value="<?php echo $checkbox_data;?>"]').attr('checked','checked');
<?php
}
}
} else
{
echo "No checkbox checked";
}
?>
})
</script>
它做什么?它将选择这些复选框,这些值是我们从$_POST中获得的。
尝试将值放入如下数组:
var values = new Array();
$.each($("input[name='meta_data_array_search_criteria[custom_profile_type][]']:checked"), function() {
values.push($(this).val());
});
您正在使用POST将该表单发送到新的URL。所以你需要用PHP而不是JavaScript来捕获所有的值。在"…/pg/members/searchuser"你只需要查询所有$_POST,然后用这些值选择你的复选框
您可以检查这个示例,它将根据post
显示选中的值 <?php
if (isset($_POST['meta_data_array_search_criteria'])){
$data = $_POST['meta_data_array_search_criteria'];
if (is_array($data['custom_profile_type'])){
//check and display checked value
if (in_array('39242', $data['custom_profile_type']))
echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39242" class="messageCheckbox" checked><span>Job Seeker</span></p>';
else
echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39242" class="messageCheckbox"><span>Job Seeker</span></p>';
if (in_array('39243', $data['custom_profile_type']))
echo '<p class="skills_odd1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39243" class="messageCheckbox" checked><span>Employer</span></p>';
else
echo '<p class="skills_odd1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39243" class="messageCheckbox"><span>Employer</span></p>';
if (in_array('39449', $data['custom_profile_type']))
echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39449" class="messageCheckbox" checked><span>college</span></p>';
else
echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39449" class="messageCheckbox"><span>college</span></p>';
}
else{
//display all unchecked
echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39242" class="messageCheckbox"><span>Job Seeker</span></p>';
echo '<p class="skills_odd1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39243" class="messageCheckbox"><span>Employer</span></p>';
echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39449" class="messageCheckbox"><span>college</span></p>';
}
}
?>