如何从列表中的选择元素获取所有选项 [英] How to get all the options from a select element in a list

查看:74
本文介绍了如何从列表中的选择元素获取所有选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jquery(或本机js).我如何在列表中获取所有选项html,所以输出如下:

Using jquery (or native js). How do I get all the options html in a list, so the output is something like:

var options = ['<option value="" selected="">---------</option>', <option value="1">Option 1</option>']

推荐答案

您可以获取选择节点并在选择节点中找到所有选项,并从这些选项中获取html.

You can get the select node and find all the options in the select node and get the html from those options.

var selectNode = $('select-selector');
var options = selectNode.find('option').toArray().map(function (o) { return o.outerHTML});

根据 Rory 的评论中的建议进行编辑.

Edit as per suggestion from comment by Rory.

$.map(selectNode.find('option'), function(o) { return o.outerHTML; });

这篇关于如何从列表中的选择元素获取所有选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆