JQuery serializeArray给一个名为query的附加值

时间:2017-10-10 15:40:41

标签: javascript jquery

这是我的HTML代码:

<form onSubmit="return false;" id="patientReferConsultant">
    <div class="portlet box blue-hoki">
        <div class="portlet-title">
            <div class="caption">Refer Consultant </div>
        </div>
        <div class="portlet-body">
            <div class="row">
                <div class="col-md-4">
                    <span for="referDoctorSpecialist" class="help-block">Specialist (Area of Practice)</span>
                    <select class="bs-select form-control" data-style="blue" id="referDoctorSpecialist" name="referDoctorSpecialist">
                        <option selected disabled value="">Select Specialist</option>
                        @if(isset($types))
                            @foreach($types as $type)
                                <option value="{{ $type['_id'] }}">{{ ucwords($type['type']) }}</option>
                            @endforeach
                            @else
                                <option value="">No Types Found</option>
                        @endif
                    </select>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-md-4">
                    <span for="referDoctor" class="help-block">Select Consultant / Doctor</span>
                    {{-- <select class="bs-select form-control" id="referDoctor" data-style="blue" data-title="Select Consultant/Doctor" name="referDoctor"> --}}
                    <select class="form-control" id="referDoctor" name="referDoctor">
                        <option selected disabled value="">Select Consultant / Doctor</option>
                    </select>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-md-4">
                    <span class="help-block">Select Clinic / Hospital (Optional)</span>
                    <select class="bs-select form-control" data-style="blue" id="referClinic" name="referClinic">
                        <option selected disabled value="">Select Clinic</option>
                        @if(isset($clinics))
                            @foreach($clinics as $clinic)
                                <option value="{{ $clinic['_id'] }}">
                                    {{ ucwords($clinic['clinic_name']) }}
                                </option>
                            @endforeach
                            @else
                        @endif
                    </select>
                </div>
            </div>
            {{--Notes--}}
            <div class="row">
                <div style="margin-top: 20px;"></div>
                <div class="col-md-4">
                    <div class="form-group form-md-line-input form-md-floating-label">
                        <textarea id="referNotes" name="referNotes" class="form-control" rows="3"></textarea>
                        <label for="form_control_1">Notes (if any)</label>
                        <span class="help-block">Additional Notes...</span>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4 col-xs-12 col-xs-12">
                    <div class="clearfix">
                        <div class="mt-35">
                            <div class="col-sm-6 col-xs-6 nopad">
                                <button id="btnSubmitPatientReferConsultant" class="btn green-meadow btn-circle fwb">
                                    <i class="fa fa-check"></i> Display on prescription
                                </button>
                            </div>
                            <div class="col-sm-6 col-xs-6 nopad">
                                <button id="cancel" type="button" class="btn btn-outline dark btn-circle fwb"> <i class="fa fa-close"></i> Cancel</button>
                            </div>
                        <div class="clearfix"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

这是我正在执行的Jquery代码:

var data = $('form').serializeArray();
console.log(data);

我得到以下输出:

(5) [{…}, {…}, {…}, {…}, {…}]
0:{name: "query", value: ""}
1:{name: "referDoctorSpecialist", value: "59d53743d9b2508325ceaf41"}
2:{name: "referDoctor", value: "59d537f9d9b2508325ceaff6"}
3:{name: "referClinic", value: "59d7a9046c73d4c582ae1e88"}
4:{name: "referNotes", value: "asdasdasd"}
length:5
__proto__:Array(0)

我不知道查询参数自动出现在哪里,如何删除它? 每当我使用上面的jquery代码时,array [0]中的第一个值就会让我查询最糟糕的事情,那个页面中没有更多的输入字段。那么查询自动生成在哪里?

1 个答案:

答案 0 :(得分:3)

  

如何删除?

jQuery可以序列化元素集合而不是表单。因此,您可以选择表单中除:input之外的所有query

var data = $("form :input:not([name=query])").serializeArray();
console.log(data);

或与!=属性选择器相同,而不是:not()

var data = $("form :input[name!=query]").serializeArray();
console.log(data);