admin管理员组

文章数量:1290535

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>                      

P.S: I am using bootstrap and jquery in my application.

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>                      

P.S: I am using bootstrap and jquery in my application.

Share Improve this question asked Nov 30, 2018 at 14:35 KashifKashif 6852 gold badges8 silver badges16 bronze badges 1
  • 1 possible duplicate:stackoverflow./questions/8641729/… – Prasad Telkikar Commented Nov 30, 2018 at 14:38
Add a ment  | 

2 Answers 2

Reset to default 3

I think you want somethings like this :

$('select[multiple]').multiselect()
<!DOCTYPE html>
<html>
<head>
<link href="https://cdnjs.cloudflare./ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare./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./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare./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>

本文标签: javascripthtml Select multiple option with simple click without pressing CTRL buttonStack Overflow