Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery访问多选下拉菜单_Jquery - Fatal编程技术网

JQuery访问多选下拉菜单

JQuery访问多选下拉菜单,jquery,Jquery,我正在使用下面提到的多选下拉菜单 <select class="form-input name="hideLineItemColumns_quote" multiple="true" "> <option selected="true" value="part_desc">Description</option> <option selected="true" value="part_number">Product</opti

我正在使用下面提到的多选下拉菜单

<select class="form-input name="hideLineItemColumns_quote" multiple="true" ">
    <option selected="true" value="part_desc">Description</option>
    <option selected="true" value="part_number">Product</option>
    <option selected="true" value="costEa_line">Cost</option>
</select>
它不显示“描述”,而是显示“D”。它将所有三个值组合在一个长字符串中。你知道我做错了什么吗

谢谢,
Nitesh是一个字符串。因此,您正在查看该字符串中的第一个字符,即
D

此外,还将选择所有选项

也许这就是你想要做的:

var tempVar = [];
jQuery("Select[@name='hideLineItemColumns_quote'] option:selected").each(function () {
                  tempVar.push($(this).text());
});
alert(tempVar[0]);

当前查询正在选择所有三个
选项
元素。当您调用这个集合时,jQuery假定您希望将这三个集合的文本合并在一起

如果要单独处理这些问题,可以通过或进行迭代:


我玩过一点

var tempVar = [];
$('select[name="hideLineItemColumns_quote"] option:selected').each(function() {
    var $option = $(this);
    tempVar.push({ text: $option.text(), value: $option.attr('value') });
});
alert(tempVar[0].text);

和其他答案一样,它的基本功能是创建列表中选定项目的数组。对于每个选定的项目,它会创建一个包含
文本
属性的小对象,以便您以后可以更直观地访问它们。(类似于
警报()
行)

有意义。但是,它不应该返回字符串,而应该返回一个包含三个值的数组:Description、Product和Cost。是否有其他方法可以单独访问所有选定的值?上面的代码工作正常,但页面上还有其他选择标记,逻辑正在提取first Select标记的第一个选定值,而不是我特别提到的(@name='hideLineItemColumns_quote')。我需要指定其他内容吗?是的,您在这条语句中缺少了一个结束语
我解决了这个问题,即使是相同的问题。查看这个fiddle@Nitesh,让我们了解一下为什么它在警报消息jsfiddle.net/efh4a/2中显示SS而不是描述?
// with .each():
$("select[@name='hideLineItemColumns_quote'] option:selected").each(function(idx, el) {
    console.log($(el).text());
});

// with .map():
var options_arr = $("select[@name='hideLineItemColumns_quote'] option:selected").map(function(idx, el) {
    return $(el).text();
}); // => ['Description', 'Product', 'Cost']
console.log(options_arr);
var tempVar = [];
$('select[name="hideLineItemColumns_quote"] option:selected').each(function() {
    var $option = $(this);
    tempVar.push({ text: $option.text(), value: $option.attr('value') });
});
alert(tempVar[0].text);