Created
June 19, 2017 08:51
-
-
Save afiru/29023009574ffd08c5657486d7b63b9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>チェックされたものをjsでカンマ区切りにして、actionを変更していく。</title> | |
</head> | |
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<body> | |
<h2>JSでフォームのチェックボックスを利用して、category.phpに値を送る方法</h2> | |
<p>フォーム内のチェックボックスにチェックを入れる度にカテゴリーの値が入り、カテゴリーの複数検索ができるようになります。</p> | |
<script> | |
$(function() { | |
$('input[name="category[]"]').change(function() { | |
var categories = []; | |
$('input[name="category[]"]:checked').each(function() { | |
categories.push($(this).val()); | |
}); | |
console.log(categories); | |
$('#p01').text('<?php echo home_url('/'); ?>?cat=' + categories + 'として送信します。'); | |
$('#test_form').attr('action','<?php echo home_url('/'); ?>?cat' + categories); | |
}); | |
}); | |
</script> | |
<form id="test_form" name="ansform" method="get" action="<?php echo home_url('/'); ?>"> | |
<?php $categories = get_categories( 'parent=取得したい親カテゴリーID' ) ); ?> | |
<?php if(!empty($categories)): ?> | |
<ul> | |
<?php foreach($categories as $category): ?> | |
<li><input type="checkbox" name="category[]" value="<?php echo $category->cat_ID; ?>"><?php echo $category->name; ?></li> | |
<?php endforeach; ?> | |
</ul> | |
<?php endif; ?> | |
<input type="submit" value="送信します。"> | |
<!--出力用(実際は削除していただいたほうがいいです)--> | |
<p id="p01"></p> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment