如何使用表单提交和 ajax 来编辑文本 Name & Sex ?? 当我单击第一个提交按钮时,它只会更改第一个姓名和性别如果单击第二个,只需更改第二个姓名和性别!怎么做 ?
HTML & JS
<!-- Form1 -->
<form action="index.php" class="form_edit" >
Name: <span class="display">Tom</span><br>
Sex: <span class="display_sex">Male</span><br>
<input type="text" name="name" value="" /><br>
<input type="text" name="sex" value="" /><br>
<input name="submit" type="submit" value="Submit" /><br><br>
</form>
<!-- Form2 -->
<form action="index.php" class="form_edit" >
Name: <span class="display">Mary</span><br>
Sex: <span class="display_sex">Female</span><br>
<input type="text" name="name" value="" /><br>
<input type="text" name="sex" value="" /><br>
<input name="submit" type="submit" value="Submit" /><br><br>
</form>
<!-- Form3 -->
<form action="index.php" class="form_edit" >
Name: <span class="display">John</span><br>
Sex: <span class="display_sex">Male</span><br>
<input type="text" name="name" value="" /><br>
<input type="text" name="sex" value="" /><br>
<input name="submit" type="submit" value="Submit" /><br><br>
</form>
<script type="text/javascript">
$('.form_edit').submit(function(){
$.ajax({
type: "POST",
url: "edit.php",
data: $(this).serialize(),
success: function(data)
{
$('.display').text(data);
}
});
return false;
});
</script>
PHP
<?php
if($_POST['name']){
$return = '{"name":"' . $_POST['name'] . '"}';
echo $return;
}
?>