【发布时间】:2010-08-17 19:38:37
【问题描述】:
目前我有以下代码:
home.php
<form name='myformname' id='myformid'>
<input type='text' name='mytext1' value='abc'>
<input type='text' name='mytext2' value='123'>
<input type='submit' value='Submit'>
</form>
<div id='textone'></div><div id='texttwo'></div>
_home.php
$arr = array( 'textone' => $_POST['mytext1'], 'texttwo' => $_POST['mytext2'] );
echo json_encode( $arr );
ajax.js
jQuery('#myformid').live('submit',function(event) {
$.ajax({
url: '_home.php',
type: 'POST',
data: $('#myformid').serialize(),
success: function( data ) {
// TODO: write code here to get json data and load DIVs instead of alert
alert(data);
}
});
return false;
});
提交时的输出:
{"textone":"abc","texttwo":"123"}
问题
我想在 textone DIV 中加载 mytext1 值,在 texttwo DIV 中使用 json 数据加载 mytext2 值_home.php
提示:我正在使用this answer 对链接点击事件执行相同的任务。但是如何在表单提交时做到这一点?
谢谢
【问题讨论】:
-
试试:
alert(data['textone']); -
@sarfraz:它被硬编码为一个 div。我想要使用 json 数据循环所有 DIV。