2016-07-29 90 views
1

我已經實現了comobox,但我的問題是我想爲我的項目實現兩個組合框,並且我想爲它們分配不同的ID,如<select id="size1"。如何分配id到這<select data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6">,如果我分配id到HTML中的選擇標記我怎麼能做到這一點在jQuery代碼$(".chosen-select").chosen();如何分配id到選擇標記

我應該改變jQuery代碼,如果我添加ID選擇標籤?

下面是我的代碼

$(function(){ 
 
    $(".chosen-select").chosen(); 
 
});
<link rel="stylesheet" href="http://harvesthq.github.io/chosen/chosen.css" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script> 
 
    <script src="http://harvesthq.github.io/chosen/chosen.jquery.js" type="text/javascript"></script> 
 
<div> 
 
      <em>Multiple Select with Groups</em><br /> 
 
      <select data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6"> 
 
      <option value="" /> 
 
      <optgroup label="NFC EAST"> 
 
       <option />Dallas Cowboys 
 
       <option />New York Giants 
 
       <option />Philadelphia Eagles 
 
       <option />Washington Redskins 
 
      </optgroup> 
 
      <optgroup label="NFC NORTH"> 
 
       <option />Chicago Bears 
 
       <option />Detroit Lions 
 
       <option />Green Bay Packers 
 
       <option />Minnesota Vikings 
 
      </optgroup> 
 
      <optgroup label="NFC SOUTH"> 
 
       <option />Atlanta Falcons 
 
       <option />Carolina Panthers 
 
       <option />New Orleans Saints 
 
       <option />Tampa Bay Buccaneers 
 
      </optgroup> 
 
      <optgroup label="NFC WEST"> 
 
       <option />Arizona Cardinals 
 
       <option />St. Louis Rams 
 
       <option />San Francisco 49ers 
 
       <option />Seattle Seahawks 
 
      </optgroup> 
 
      <optgroup label="AFC EAST"> 
 
       <option />Buffalo Bills 
 
       <option />Miami Dolphins 
 
       <option />New England Patriots 
 
       <option />New York Jets 
 
      </optgroup> 
 
      <optgroup label="AFC NORTH"> 
 
       <option />Baltimore Ravens 
 
       <option />Cincinnati Bengals 
 
       <option />Cleveland Browns 
 
       <option />Pittsburgh Steelers 
 
      </optgroup> 
 
      <optgroup label="AFC SOUTH"> 
 
       <option />Houston Texans 
 
       <option />Indianapolis Colts 
 
       <option />Jacksonville Jaguars 
 
       <option />Tennessee Titans 
 
      </optgroup> 
 
      <optgroup label="AFC WEST"> 
 
       <option />Denver Broncos 
 
       <option />Kansas City Chiefs 
 
       <option />Oakland Raiders 
 
       <option />San Diego Chargers 
 
      </optgroup> 
 
      </select> 
 
     </div>

回答

1

那是微不足道的。這是你的預期?

$(function(){ 
 
    $("#select1").chosen(); 
 
    $("#select2").chosen(); 
 
});
<link rel="stylesheet" href="http://harvesthq.github.io/chosen/chosen.css" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script> 
 
    <script src="http://harvesthq.github.io/chosen/chosen.jquery.js" type="text/javascript"></script> 
 
<div> 
 
      <em>Multiple Select with Groups</em><br /> 
 
      <select id="select1" data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6"> 
 
      <option value="" /> 
 
      <optgroup label="NFC EAST"> 
 
       <option />Dallas Cowboys 
 
       <option />New York Giants 
 
       <option />Philadelphia Eagles 
 
       <option />Washington Redskins 
 
      </optgroup> 
 
      <optgroup label="NFC NORTH"> 
 
       <option />Chicago Bears 
 
       <option />Detroit Lions 
 
       <option />Green Bay Packers 
 
       <option />Minnesota Vikings 
 
      </optgroup> 
 
      <optgroup label="NFC SOUTH"> 
 
       <option />Atlanta Falcons 
 
       <option />Carolina Panthers 
 
       <option />New Orleans Saints 
 
       <option />Tampa Bay Buccaneers 
 
      </optgroup> 
 
      <optgroup label="NFC WEST"> 
 
       <option />Arizona Cardinals 
 
       <option />St. Louis Rams 
 
       <option />San Francisco 49ers 
 
       <option />Seattle Seahawks 
 
      </optgroup> 
 
      <optgroup label="AFC EAST"> 
 
       <option />Buffalo Bills 
 
       <option />Miami Dolphins 
 
       <option />New England Patriots 
 
       <option />New York Jets 
 
      </optgroup> 
 
      <optgroup label="AFC NORTH"> 
 
       <option />Baltimore Ravens 
 
       <option />Cincinnati Bengals 
 
       <option />Cleveland Browns 
 
       <option />Pittsburgh Steelers 
 
      </optgroup> 
 
      <optgroup label="AFC SOUTH"> 
 
       <option />Houston Texans 
 
       <option />Indianapolis Colts 
 
       <option />Jacksonville Jaguars 
 
       <option />Tennessee Titans 
 
      </optgroup> 
 
      <optgroup label="AFC WEST"> 
 
       <option />Denver Broncos 
 
       <option />Kansas City Chiefs 
 
       <option />Oakland Raiders 
 
       <option />San Diego Chargers 
 
      </optgroup> 
 
      </select> 
 
     </div> 
 

 
<div> 
 
      <em>Multiple Select with Groups</em><br /> 
 
      <select id="select2" data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6"> 
 
      <option value="" /> 
 
      <optgroup label="NFC EAST"> 
 
       <option />Dallas Cowboys 
 
       <option />New York Giants 
 
       <option />Philadelphia Eagles 
 
       <option />Washington Redskins 
 
      </optgroup> 
 
      <optgroup label="NFC NORTH"> 
 
       <option />Chicago Bears 
 
       <option />Detroit Lions 
 
       <option />Green Bay Packers 
 
       <option />Minnesota Vikings 
 
      </optgroup> 
 
      <optgroup label="NFC SOUTH"> 
 
       <option />Atlanta Falcons 
 
       <option />Carolina Panthers 
 
       <option />New Orleans Saints 
 
       <option />Tampa Bay Buccaneers 
 
      </optgroup> 
 
      <optgroup label="NFC WEST"> 
 
       <option />Arizona Cardinals 
 
       <option />St. Louis Rams 
 
       <option />San Francisco 49ers 
 
       <option />Seattle Seahawks 
 
      </optgroup> 
 
      <optgroup label="AFC EAST"> 
 
       <option />Buffalo Bills 
 
       <option />Miami Dolphins 
 
       <option />New England Patriots 
 
       <option />New York Jets 
 
      </optgroup> 
 
      <optgroup label="AFC NORTH"> 
 
       <option />Baltimore Ravens 
 
       <option />Cincinnati Bengals 
 
       <option />Cleveland Browns 
 
       <option />Pittsburgh Steelers 
 
      </optgroup> 
 
      <optgroup label="AFC SOUTH"> 
 
       <option />Houston Texans 
 
       <option />Indianapolis Colts 
 
       <option />Jacksonville Jaguars 
 
       <option />Tennessee Titans 
 
      </optgroup> 
 
      <optgroup label="AFC WEST"> 
 
       <option />Denver Broncos 
 
       <option />Kansas City Chiefs 
 
       <option />Oakland Raiders 
 
       <option />San Diego Chargers 
 
      </optgroup> 
 
      </select> 
 
     </div>

+0

感謝,我該怎麼做選擇標籤我的意思是HTTP預期的行爲:// stackoverflow.com/questions/38619548/how-to-choose-the-text-without-selection-in-jquery-chosen-plugin – overflowstack9

+0

我想要這樣http://stackoverflow.com/questions/38619548/how-to-如果你不介意 – overflowstack9

+1

肯定會選擇-text-without-selection-in-jquery-chosen-plugin,我會看看t帽子也... – kukkuz

1

您可以在選定的標籤集ID。

在HTML像這樣:

<select id="size1" data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6"></select> 

<select id="size2" data-placeholder="Team 2" style="width:350px;" class="chosen-select" multiple tabindex="6"></select> 

在jQuery中,你可以調用這個ID是這樣的:

$(function(){ 
    $("#size1").chosen(); 
    $("#size2").chosen(); 
});