簡體   English   中英

jQuery | 如何從select html元素中獲取所選項目

[英]jQuery | How to get selected items from select html element

我在頁面中設置了一個選擇控件,如下所示:

<select multiple="multiple" name="name[]" id="ListId">
    <optgroup label="Group 1">
        <optgroup label="Group 1.1">
            <option selected="selected" value="1">One</option>
            <option value="2">Two</option>
            <option selected="selected" value="3">Three</option>
        </optgroup>
        <optgroup label="Group 1.2">
            <option value="1">One</option>
            <option value="2">Two</option>
            <option selected="selected" value="3">Three</option>
        </optgroup>
    </optgroup>
    <optgroup label="Group 2">
        <optgroup label="Group 2.1">
            <option value="1">One</option>
            <option selected="selected" value="2">Two</option>
            <optionvalue="3">Three</option>
        </optgroup>
    </optgroup>
</select>

通過在加載頁面時使用jQuery,我嘗試通過以下代碼獲取所選項:

if($('#ListId').length > 0)
{
    var selected = [];

    $('#ListId option[selected="selected"]').each(
        function(i)
        {
            var val = $(this).val();
            var txt = $(this).text();

            selected[val] =   txt;
        }
    );
}

然后我修改上面的代碼以獲取所選項目,如果用戶進行了如下新選擇:

if($('#ListId').length > 0)
{
    var selected = [];

    $('#ListId option[selected="selected"]').each(
        function(i)
        {
            var val = $(this).val();
            var txt = $(this).text();

            selected[val] =   txt;
        }
    );

    $('#ListId').change(
        function()
        {
            selected = [];

            $('#ListId option[selected="selected"]').each(
                function(i)
                {
                    var val = $(this).val();
                    var txt = $(this).text();

                    selected[val] =   txt;
                }
            );
        }
    );
}

但問題是仍然繼續閱讀預覽“選定”項目。

您還可以訪問這里的小提琴進行實時測試: http//jsfiddle.net/Qem7n/

有關如何解決這個問題的任何想法?

更改

$('#ListId option[selected="selected"]')

$('#ListId option:selected')

因為: [selected="selected"]不是實時選擇器。

示例: http//jsfiddle.net/9YrY8/

http://jsfiddle.net/jxQuU/52/

function put(sel,i) {
    sel[i.value]=$(i).text();
}
function change() {
    var sel = [];
    $('#ListId option:selected').each(function (k,i){put(sel,i);});
    alert(sel[1]);
}
$('#ListId').on('change',change);
change();

嘗試這個:

$('#ListId').change(function(){
  var selected = $('#ListId').val()
  var str = '';
  for (var i=0;i<selected.length;i++){
    if (i>0) str += ', ';
      str += selected[i];
    }
  alert(str);
});

或這個:

$('#ListId').change(function(){
    var $selected = $('#ListId option:selected');
    var i=0;
    var str = '';
    $selected.each(function(){
    if (i>0) str+= ', ';
    str += $(this).val() + " : " + $(this).text()+ " ";
    ++i;
  });
  alert(str);
});

或者如果要將所選項目放入數組中,請使用以下命令:

$('#ListId').change(function(){
  var arr = [];
  var $selected = $('#ListId option:selected');
  //var i=0;
  //var str = '';
  $selected.each(function(){
     //if (i>0) str+= ', ';
     //str += $(this).val() + " : " + $(this).text()+ " ";
     arr[$(this).val()+$(this).parent().prop('label')] = $(this).text();
     //++i;
  });
  console.log(arr);
});

您應該考慮到選項具有相同的值,並且使用選項值作為數組的索引將導致數組上具有相同索引值的其他值被更改而不會附加到數組中。

有一個單行解決方案,只需使用它

$('#formid').find('select[name="selectname"]').val()

它應該工作,它為我做了!

和托馬斯說的一樣,你也可以使用

$('option:selected', '#ListId')

第二個參數代表搜索的范圍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM