如何将data.append与$ this.attr()混合

时间:2015-08-09 08:22:38

标签: javascript php jquery ajax

我只想学习如何在jquery ajax中将data.append()与$ this.attr()混合使用。因为我在php循环中创建了表单,每个表单都有其唯一的名称。我有这个:

var data = new FormData()
    data.append( 'photo', $( '#photo_' + $this.attr("name") + '_id' )[0].files[0] ), 
    data.append( 'firstname', $( '#firstname_' + $this.attr("name") + '_id' ).val()),
    data.append( 'lastname', $( '#lastname_' + $this.attr("name") + '_id' ).val()),
    data.append( 'fathername', $( '#fathername_' + $this.attr("name") + '_id' ).val()),
    data.append( 'nationalID', $( '#nationalID_' + $this.attr("name") + '_id' ).val()),
    data.append( 'month', $( '#month_' + $this.attr("name") + '_id' ).val()),
    data.append( 'year', $( '#year_' + $this.attr("name") + '_id' ).val()),
    data.append( 'day', $( '#day_' + $this.attr("name") + '_id' ).val()),
    data.append( 'class', $( '#class_' + $this.attr("name") + '_id' ).val()),
    data.append( 'identity', $this.attr("name").val());
$.ajax({
        type         : 'POST',
        url          : 'http://localhost/wordpress/wp-content/themes/mytheme/process/edit-student-process.php',
        processData  : false,
        contentType  : false,
        data         : data,
        enctype      : 'multipart/form-data',
        dataType     : 'json',
    })

但是使用此代码,ajax重定向到应该处理表单的php文件,而不是保留在表单上。问题出在哪里?

和我表格的一部分:

<form class="form-horizontal edit_student" name="<?php echo $student['user_ID']; ?>" action="http://localhost/wordpress/wp-content/themes/danesh-sanat/process/edit-student-process.php" enctype="multipart/form-data" method="POST">
<input type="text" name="firstname_<?php echo $student['user_ID']; ?>_id" id="firstname_<?php echo $student['user_ID']; ?>_id" class="form-control" value="<?php echo $student['user_firstname']; ?>">
<input type="text" name="lastname_<?php echo $student['user_ID']; ?>_id" id="lastname_<?php echo $student['user_ID']; ?>_id" class="form-control" value="<?php echo $student['user_lastname']; ?>">

0 个答案:

没有答案