jQuery JQuery从列表框中获取选定的项目并用逗号分割
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7388457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Get Selected Item from List Box and split by comma
提问by Justin Erswell
Can anyone offer more help on this I have the code setup on jsfiddle at...
任何人都可以提供更多帮助,我在 jsfiddle 上设置了代码...
http://jsfiddle.net/justinerswell/feDTU/
http://jsfiddle.net/justinerswell/feDTU/
I need to get the last item in the substr and work backwards to populate the fields I need.
我需要获取 substr 中的最后一项,然后向后工作以填充我需要的字段。
Thanks
谢谢
Original Question
原始问题
Hi I have a list box returned on a page with multiple result which are being parsed in as a string into the value...
嗨,我在页面上返回了一个列表框,其中包含多个结果,这些结果被作为字符串解析为值...
<option value="1 Corn Exchange, The Strand, RYE, East Sussex, TN31 7DB, UNITED KINGDOM">
Using JQuery I need to do a couple of things..
使用 JQuery 我需要做一些事情..
- Get Selected Value
- Split Value of Selected Item by Comma's
- Loop through in revers and set a variable for each item i.e country: United Kingdom Postcode:TN31 7DB etc
- 获取选定值
- 按逗号分割所选项目的值
- 反向循环并为每个项目设置一个变量,即国家/地区:英国邮政编码:TN31 7DB 等
Can anyone help me?
谁能帮我?
Thanks
谢谢
回答by Marco Johannesen
Do you want to fetch the data on click or submit?
你想在点击或提交时获取数据吗?
$('select').click( function() {
var selected = $(this).find(':selected').text();
var substr = selected.split(',');
var product = substr[0];
var city = substr[1];
var postcode = substr[2];
var country = substr[3];
//DO SOMETHING WITH IT??
});
But its not a very good idea, considering your "data" structure can't change (ie. missing fields and so on) - since it will break on the indexes....
但这不是一个好主意,考虑到您的“数据”结构无法更改(即缺少字段等)-因为它会破坏索引....
回答by Thomas
jquery has a selector for selected value.
jquery 有一个选择值的选择器。
so use
所以用
var selectedValue = $('#myselect option:selected').val();
then split on space
然后在空间上分裂
var table = selectedValue.split(',');
and loop on your table
并在你的桌子上循环
for(i = 0 ; i < table.length; i++) {
// dosomething with table[i]
}
回答by Jignesh Rajput
suppose if you are use Html Listbox than try this
假设你使用 Html Listbox 比试试这个
<select id="Select1" size="4" multiple="multiple" onchange="Check(this);">
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="0">All</option>
</select>
javascript
javascript
function Check(selectObject)
{
var items = new Array();
for (var i = 0, j = selectObject.options.length; i < j; i++)
{
if (selectObject.options[i].selected)
{
items.push(selectObject.options[i].value);
}
}
// selectObject.id holds the "id"
var itemsToString = items.toString();
alert(itemsToString);
//split and get values.
var s = itemsToString.split(',');
for(var k=0; k<s.length; k++)
{
alert(s[k]);
}
}
Hops its helps
跳它的帮助
回答by Ashish Shukla
Try $.map()to get all selected items in list
尝试$.map()以获取列表中的所有选定项目
var selectedValues = $.map($('#ddlList option:selected'), function (element) {
return element.value;
});
回答by Ravinder Singh Bhanwar
Try this simple, call this function onchange() event of dropdown listbox .
试试这个简单的方法,调用下拉列表框的 onchange() 事件函数。
function FuncFilter() {
函数 FuncFilter() {
var selectedText = $("#DrpDatefilter").find(':selected').text();
alert(selectedText );
**Or**
var selectedValue = $("#DrpDatefilter").find(':selected').val();
alert(selectedValue );
}