【问题标题】:Serialize form data return empty string序列化表单数据返回空字符串
【发布时间】:2018-06-07 11:01:23
【问题描述】:

如果我的问题重复,我深表歉意。我研究了所有相同的问题并做了以下事情: 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>

这会扼杀我的一天。我想我在做一些愚蠢的类型错误。请指出。

注意:我尝试删除隐藏字段

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

【问题讨论】:

标签: serializearray


【解决方案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>

【讨论】:

  • 是否可以在不点击任何按钮的情况下做同样的事情?
  • @AbdusSattarBhuiyan 是的,因为序列化是针对表单元素的,而不是针对提交按钮的。您可以致电console.log($('#question_paper').serializeArray()),它也可以像魅力一样工作,无需任何点击:)
猜你喜欢
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 2013-01-06
相关资源
最近更新 更多