【发布时间】:2015-11-06 04:01:42
【问题描述】:
我正在使用带有 JQuery 的 ajax 来序列化我的表单并将数据发送到我的 insert.php 页面,以便它插入到我的数据库中。
我正在删除表单 form.serialize 中的所有空输入。这是为了确保将这些值作为 NULL 插入到我的数据库中。
我假设,我必须为每个输入设置一个默认值 none,以使这个过程正常工作。
但是,当我这样做时,我发现当表单被序列化时,那些设置为空默认值的输入不会被发送。似乎 .serialize 发送输入的默认值,而不是用户在输入中键入的内容。
我该如何解决这个问题?
以下是我如何在表单中设置具有空白默认值的输入:
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Item Price</label>
<div class="input-icon right">
<i class="fa"></i>
<input type="text" id="item_price" name="item_price" class="currency_mask form-control" value="">
</div>
</div>
</div>
以下是我在 ajax 中省略 .serialize 中的空值的方法:
$.ajax({
url: "ajax_insert.php?table=invoice_item",
type: "post",
dataType: 'json',
data: $("#item_form :input[value!='']").serialize(), //remove blank inputs so they aren't passed into the db and are inserted as NULL
success: ....
无论在“项目价格”字段中输入/输入什么内容,都会使用表单中的默认值。因为默认值是空,所以从 .serialize 中省略。
我绝对需要从序列化中省略空白输入,以便在我的数据库中将该字段设置为 NULL。所以这就是我在表单被序列化之前省略它们的原因。
【问题讨论】:
-
我觉得应该是
$(this).serialize()
标签: php jquery ajax serialization