search

Home  >  Q&A  >  body text

Select2 with checkbox list for multiple selection but not getting values

<p>使用此代码,它可以工作。但没有在 $('form').serialize(); 中获取选定的值</p> <pre class="brush:php;toolbar:false;">$('.select2-multiple').select2MultiCheckboxes({ placeholder: "Choose multiple elements", }) <div class="row"> <form id="search_vendor_form"> <select name="sel-01[]" id="sel-01" class="select2-multiple"> <option></option> <option value="AL">Alabama</option> <option value="CA">California</option> <option value="NY">New York</option> <option value="TX">Texas</option> <option value="WY">Wyoming</option> </select> </div> </form> <div class="row"> </div> <div class="row"> </div> jQuery(function($) { $('.select2-multiple').select2MultiCheckboxes({ placeholder: "Choose multiple elements", })`your text` }); $(document).on('change','#sel-01',function(){ console.log($(#search_vendor_form).serialize()) })</pre> <p>我想在ajax中发送表单值,但不进入console.log($(#search_vendor_form).serialize())</p>
P粉399585024P粉399585024459 days ago597

reply all(1)I'll reply

  • P粉311563823

    P粉3115638232023-09-01 10:14:31

    • You haven't put the selector in quotes. It must be a string.
    • For some reason you seem to have "your text" as the string after the function, which is invalid and may be a bug.
    • You need to put all your JS code into the wrapper jQuery(function($){ ... )} inside the function.
    • The
    • element you opened was not closed properly, it seems you closed it before the
      element, so those two closing tags need to be switched.
    The following should solve this problem:

    HTML

    <div class="row">
      <form id="search_vendor_form">
        <select name="sel-01[]" id="sel-01" class="select2-multiple">
          <option></option>
          <option value="AL">Alabama</option>
          <option value="CA">California</option>
          <option value="NY">New York</option>
          <option value="TX">Texas</option>
          <option value="WY">Wyoming</option>
        </select>
      </form>
    </div>
    
    <div class="row">
    </div>
    <div class="row">
    </div>
    

    JS

    jQuery(function($){
      $('.select2-multiple').select2MultiCheckboxes({
          placeholder: "Choose multiple elements",
      });
    
      $(document).on('change','#sel-01',function(){
          console.log($('#search_vendor_form').serialize())
      });
    });
    

    reply
    0
  • Cancelreply