我正在使用ajax和JQuery来序列化我的表单并将数据发送到我的insert.php页面,以便将其插入到我的数据库中。JQuery .serialize发送默认值
我删除窗体form.serialize中空的任何输入。这是为了确保这些值作为NULL插入到我的数据库中。
我必须为每个输入设置一个默认值,我假设,使这个过程工作。
但是,当我这样做时,我发现当表单被序列化时,那些设置为空默认值的输入不会被发送。看起来像.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()' – aldrin27