简体   繁体   English

选择所有选项

[英]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. 或者如何选择所有选项ob提交。 I have only class of select(not ID). 我只有选择类(不是ID)。

I think you can even write your replacement line like this: 我想你甚至可以写这样的替换线:

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

to avoid the .each() loop. 避免.each()循环。

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. 这里是Kitsched代码的一个小修改,以确保如果你只有类而不是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 . 如果你有任何一个按钮的ID或选择只需替换。 with # in script. 用脚本编写。 For eg if button's id is clickme instead of class just use #clickme instead of .clickme. 例如,如果按钮的id是clickme而不是class,则只需使用#clickme而不是.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: 我不是100%肯定这是你要求的,但它在这里:

<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): 对于动态选项(使用jQuery):

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

Untested. 未经测试。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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