jquery - 使用 jQuery 选择 HTML 选择的所有值

标签 jquery html html-select

我得到一个 HTML select 选项,它是用 PHP 生成的。选项的名称是目录中的文件夹。现在我需要 jQuery 脚本中的值。我只知道命令 $('#select :selected').text()$(#select).val() 但两者都只是给我带来当前选择的选项。

使用 PHP 的选择框:

<select id="existAlbum" name="existAlbum" size="1">
    <option>SELECT ALBUM</option>
    <?PHP
        foreach ($alledateien as $datei) {

            $dateiinfo = pathinfo($album."/".$datei);
            if ($datei != "." && $datei != ".." && $datei != "background.jpg" && $datei != "loading.gif") {

                echo ('<option>' . $datei . '</option>');
            };
        };
    ?>      
</select>

是否可以添加 jQuery 数组中的所有值?
我需要将选项值与其他单词进行比较。

最佳答案

$(function(){

    var optionValues = [];

    $('#existAlbum option').each(function(){
        optionValues.push(this.value);
        //or if you want to use the jquery methods on each option:
        //$(this).val()
        //with your current code, you would need $(this).text(), 
        //since youre not adding any values
        //"this" is the current DOM element (<option>)
    });

    //do stuff with the optionValues-array here

});

关于jquery - 使用 jQuery 选择 HTML 选择的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13099063/

相关文章:

javascript - 如何知道 Html 元素何时准备好(呈现)?

javascript - AWS 和 angularjs 的问题

javascript - 如何访问在 Ajax 回调函数中初始化的变量?

javascript - if 语句中无法识别 ASCII 字符

jquery - 如何获取宽度、高度或tinymce编辑器

php - JavaScript 错误 : Object #<HTMLFormElement> has no method 'getElementById'

javascript - 在网页的单选按钮网格中,每列和每行只允许一个选择

php - 从下拉框中获取选定值的下两个值

html - 选择标签内的文本垂直未对齐

c# - MVC 下拉列表只读不起作用