序列化表单数据返回空字符串

时间:2017-12-26 16:41:49

标签: serializearray

如果我的问题重复,我道歉。我研究了所有相同的问题并做了以下事情: 1.删​​除所有禁用的输入字段。 2.检查是否有重复的id。没有id重复。 3.每个表单字段都有一个名称。 但是下面的代码返回空字符串:

$('#answer_sheet_btn').click(function(e){
        e.preventDefault();
        console.log( $( this ).serializeArray() );

  });

这是我的表格:

<form method="post" action="/wordpress/wp-admin/admin.php?page=active-exam&amp;exam_id=1" id="question_paper">
        <p></p><table class="bix-tbl-container" style="height: 40px" border="0" width="533" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="bix-td-qtxt" valign="top">If 60 J of energy are available for every 15 C of charge, what is the voltage?</td>
</tr>
</tbody>
</table><p></p>
    <input name="opt1[]" type="checkbox" value="1">60 V <input name="opt1[]" type="checkbox" value="2">4 V  <input name="opt1[]" type="checkbox" value="3">15 V <input name="opt1[]" type="checkbox" value="4">.25 V    <hr>

    <p>Which resistive component is designed to be temperature sensitive?</p>
    <input name="opt2[]" type="checkbox" value="1">Rheostat <input name="opt2[]" type="checkbox" value="2">Thermistor   <input name="opt2[]" type="checkbox" value="3">Potentiometer    <input name="opt2[]" type="checkbox" value="4">Photoconductive cell <hr>

<input type="hidden" name="q_ids" value="1,2">
<p class="submit">
    <input type="submit" name="answer_sheet" id="answer_sheet_btn" class="button-primary" value="submit">
</p>

</form>

这杀了我的一天。我想我在做一些愚蠢的错误。请指出。

N.B:我尝试删除隐藏的字段

<input type="hidden" name="q_ids" value="1,2">

1 个答案:

答案 0 :(得分:1)

根据.serializeArray()

  

.serializeArray()方法创建一个JavaScript对象数组,可以编码为JSON字符串。它运行在jQuery表单和/或表单控件集合上。

您应该处理表单提交操作,然后序列化表单:

$('#question_paper').submit(function(e){
    e.preventDefault();
    console.log( $(this).serializeArray() );
});

$('#question_paper').submit(function(e){
    e.preventDefault();
    console.log( $(this).serializeArray() );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="/wordpress/wp-admin/admin.php?page=active-exam&amp;exam_id=1" id="question_paper">
  <p></p>
  <table class="bix-tbl-container" style="height: 40px" border="0" width="533" cellspacing="0" cellpadding="0">
<tbody>
  <tr>
    <td class="bix-td-qtxt" valign="top">If 60 J of energy are available for every 15 C of charge, what is the voltage?</td>
  </tr>
</tbody>
  </table>
  <p></p>
  <input name="opt1[]" type="checkbox" value="1">60 V
  <input name="opt1[]" type="checkbox" value="2">4 V
  <input name="opt1[]" type="checkbox" value="3">15 V
  <input name="opt1[]" type="checkbox" value="4">.25 V
  <hr>

  <p>Which resistive component is designed to be temperature sensitive?</p>
  <input name="opt2[]" type="checkbox" value="1">Rheostat
  <input name="opt2[]" type="checkbox" value="2">Thermistor
  <input name="opt2[]" type="checkbox" value="3">Potentiometer
  <input name="opt2[]" type="checkbox" value="4">Photoconductive cell
  <hr>

  <input type="hidden" name="q_ids" value="1,2">
  <p class="submit">
<input type="submit" name="answer_sheet" id="answer_sheet_btn" class="button-primary" value="submit">
  </p>

</form>