cakePHP+JQueryでajaxを使ってFormの内容をPOSTしてみた。その2(修正版)

Twitter ツイート Hatena Bookmark ブックマーク

最新情報!!!!
新しく修正した記事書きました!!!
cakePHP+JQueryでajaxを使ってFormの内容をPOSTしてみた。その3(修正版)

前に書いた記事がselectやradioなどが存在する場合に動作しない事が判明したため、修正版を用意しました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

$(function(){

	$('form').submit(function(){
		var postData = {};
		$('form').find(':input').each(function(){
			if ( !$(this).is(':disabled') ) {
				// disableでない場合のみサーバに送る
				if ( $(this).attr('type') == 'radio'  || $(this).attr('type') == "checkbox" ) {
					// ラジオボタンの場合
					if ( $(this).is(':checked') ) {
						postData[$(this).attr('name')] = $(this).val();
					}
				}
				else if ( $(this).get(0).tagName == "SELECT" ) {
					// セレクトボックスの場合
					postData[$(this).attr('name')] = $(this).val();
				}
				else {
					// その他
					postData[$(this).attr('name')] = $(this).val();
				}
			}
		});
		$.post('/post/add',postData,function(data){
			//...callback処理
		});
		return false;
	});
});
<?php $this->Html->scriptEnd()?>
<?php echo $this->Form->create('Post');?>
<?php echo $this->Form->input('title');?>
<?php echo $this->Form->input('name');?>
<?php echo $this->Form->input('description');?>
<?php echo $this->Form->select('selectlist', array('a' => 'あ','b' => 'う'));?>
<?php echo $this->Form->select('selectlist_m', array(	'a' => 'あ','b' => 'う'),null,array('multiple' => 'multiple'));?>
<?php echo $this->Form->end('登録');?>

これで正しく動作するかと思います。
ちゃんとデバッグしてないので誰かデバッグしてくれるとうれしいっす。

comments powered by Disqus
Built with Hugo
テーマ StackJimmy によって設計されています。