2017-10-10 50 views
0

附加值這是我的HTML代碼:JQuery的serializeArray gving命名查詢

<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代碼的數組[0]中的第一個值,我會查詢最糟糕的事情,那裏沒有更多的輸入字段。那麼查詢自動生成在哪裏?

+2

很難沒有看到你的HTML說,但在猜測它可能是從一個隱藏的輸入到來,或按鈕 –

+0

請添加HTML –

+0

可能是一個搜索框?嘗試輸入任何內容並重新提交。 – numbtongue

回答

3

如何將其刪除?

jQuery可以序列化一組元素而不是表單。因此,您可以選擇除名爲query之外的所有:input

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

或者用!=屬性選擇,而不是:not()相同。

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

完美工作 –

+0

太棒了。很高興幫助。 – llama

+0

此外,我想要在此代碼中進行修改,我在HTML代碼中有一個選擇字段,因爲它只是在註釋上方可選,所以我希望該值也可以被禁用 –