简体   繁体   中英

html Select multiple option with simple click without pressing CTRL button

In my Laravel app, I have a form to select multiple options. However, I don't want to select multiple options by pressing CTRL and then selecting them. I just want to be able simply click on the options and they should be selected. And, if I click on a selected option again, then it should be de-selected.

How can I achieve this task? Here is an example of my select options list.

 <div class="form-group row">
  <label class="col-form-label" for="selectednames[]">Select Name</label>
 </div>
<div class="form-group row">                      
<select multiple name="selectednames[]">
  <option value="1">John</option>
  <option value="2">Sam</option>
  <option value="3">Max</option>
  <option value="4">Shawn</option>
</select>                      

PS: I am using bootstrap and jquery in my application.

I think you want somethings like this :

 $('select[multiple]').multiselect() 
 <!DOCTYPE html> <html> <head> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css" /> </head> <body> <select multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.min.js"></script> </body> </html> 

simply use checkbox type as input instead of option, like that:

<div class="form-check">
    <input type="checkbox" name="selectednames[]" value = "1" />
    <input type="checkbox" name="selectednames[]" value = "2" />
    <input type="checkbox" name="selectednames[]" value = "3" />
    <input type="checkbox" name="selectednames[]" value = "4" />
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM