【问题标题】:Ajax Serialize Not Getting All Values of formAjax 序列化未获取表单的所有值
【发布时间】:2014-10-18 12:15:01
【问题描述】:

所以我有form 有一些输入字段,它通过Ajax Serialize 提交。 问题是它没有在serialize output中获取表单的所有值。我知道我已经为每个字段提供Name,这是我的HTML代码:

<form action="" id="BindData">
        <input type="hidden" name='style_id' id='style_id' value="<?php echo $model->style_id; ?>">  
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">Select Category:</span>
                <select id="category" name="cat_id" class="form-control">
                    <option value="">Select Category</option>
                    <?php foreach($all_categories['models'] as $Category) { ?>    
                    <option value="<?php echo $Category->category_id; ?>"><?php echo $Category->category_name;  ?></option>
                    <?php } ?>
                </select>
            </div>
        </div>
        <div class="form-group tr_sub_cat" style="display:none;">
            <div class="input-group">
                <span class="input-group-addon">Select SubCategory:</span> 
            </div>
            <div class="input-group">
                <span class="sub_category"></span>
            </div>
        </div>
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon">Weightage:</span>
                <div class="col-xs-1">
                    <input type="text" name="weightage" id="weightage" class="form-control" value="0"><span class="badge bg-red">%</span>
                </div>
            </div>
        </div>
<button type="button" class="btn btn-primary addBind pull-left">Submit</button>
 </form> 

在我上面的代码中,序列化方法没有采用cat_idweightage 的值,而我已经给了它们name。 从cat_id 选择框加载的子类别正在序列化数据中,并且权重值的更改未输出。 这是我的序列化输出:

style_id=1&cat_id=&sub_cat=95&weightage=0 

cat_id 是空白,weightage 仍然是 0,而不是更新后的。 提前致谢。

【问题讨论】:

    标签: php jquery ajax forms serialization


    【解决方案1】:

    您可以使用 val 函数从输入中收集数据:

    var cat =  $("#cat_id").val();
    var weightage =  $("#weightage").val();
    

    您不必为每个字段手动执行此操作。这就是序列化的用途。 您可以使用上面的代码进行调试,以检查是否保存在变量中的正确表单值。

    【讨论】:

    • 使用此方法的值即将到来
    猜你喜欢
    • 2011-09-05
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2018-06-08
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多