简体   繁体   中英

Select all options in select

I have a multiple select. How can make, that all options will be selected always? Or how make a selecting all options ob submit. I have only class of select(not ID).

I think you can even write your replacement line like this:

$(".someClass option").attr("selected", "selected");

to avoid the .each() loop.

Here's a slight modific to Kitsched's code to ensure that you're able to select the button/select control if you only have the class and not the id.

<html id="html">
<body id="body">
<script src="file:\\\D:\Sidharth\javascript\jquery-1.4.2.min.js"></script>

<script type="text/javascript">
    $(document).ready(function () {

        function SelectAll() {
            $(".someClass").children().each(function () { $(this).attr("selected", "selected"); });
        }

        $(".clickme").bind("click", SelectAll);


    });
</script>
<input type="submit" class="clickme" />
<select multiple="yes" class="someClass">
<OPTION VALUE="1" id="one">Option 1</OPTION> 
<OPTION VALUE="2" id="two">Option 2</OPTION> 
<OPTION VALUE="3" id="three">Option 3</OPTION> 
<OPTION VALUE="4" id="four">Option 4</OPTION> 
</select>

</div>
</body>
</html>

If you've IDs for either button or select just replace the . with # in script. For eg if button's id is clickme instead of class just use #clickme instead of .clickme.

Hope this is what you're looking for.

I'm not 100% sure that this is what you're asking for, but here it goes:

<SELECT MULTIPLE="yes" ID="multipleSelect">
<OPTION VALUE="1" SELECTED="selected">Option 1</OPTION>
<OPTION VALUE="2" SELECTED="selected">Option 2</OPTION>
<OPTION VALUE="3" SELECTED="selected">Option 3</OPTION>
<OPTION VALUE="4" SELECTED="selected">Option 4</OPTION>
<OPTION VALUE="5" SELECTED="selected">Option 5</OPTION>
<OPTION VALUE="6" SELECTED="selected">Option 6</OPTION>
</SELECT>

<INPUT TYPE="submit" ID="submit" value="Go!"/>

For the dynamic option (with jQuery):

$("#submit").click(function() {  
  $("#multipleSelect option").each(function() {
    $(this).attr("selected", "selected");
  });
});

Untested.

Good luck.

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