带有多个值的jQuery Multi-Select

杰夫·比格里(Jeff Beagley)

我有一个表单,您可以在其中选择一个位置,该位置具有一个与之相关的邮政编码,并在data-foo值中捕获。我需要的是一个基于多个位置选择的数组。

一个例子是,如果两个都被选中,我将有65807 => 71118

形式:

<form enctype='multipart/form-data' role='form' action='' method='post'>	
		<div class="row">
			<div class="col-md-3">
				<div class='form-group'>							
					<label for='select'>Destination(s)&nbsp;</label>
						<select name='destination[]' style='height: 200px;' multiple class='form-control'  multiple='multiple' id='destination' style='lane'>";
							<option value='Springfield' data-foo='65807'>Springfield, MO</option>
							<option value='Shreveport' data-foo='71118'>Shreveport, LA</option>
						</select>
				</div>
			</div>
		</div>
</form>

到目前为止,我对JS有什么:

$(function(){
    $('#origin').change(function(){
        var selected = $(this).find('option:selected');
       $('#origin_zip').html(selected.data('foo')); 
    }).change();
});

$('#destination').change(function() {
    $('#destination_zip').text('');
    
    var selected = $('#destination').val();
    for (var i = 0; i < selected.data; i++) {
       $('#destination_zip').data($('#destination_zip').data('data-foo') + selected[i]);
    }
});

编辑:

这是使用文本构建数组的代码,而不是我需要的data-foo。

$('#destination').change(function() {
    $('#destination_zip').text('');
    
    var selected = $('#destination').val();
    for (var i = 0; i < selected.length; i++) {
       $('#destination_zip').text($('#destination_zip').text() + selected[i]);
    }
});

格里芬

可以使用以下内容:

$('#destination').change(function() { // Whenever the select is changed
    var arr = []; // Create an array
    $('#destination option:selected').each(function(){ // For each selected location
        arr.push($(this).data("foo")); // Push its ZIP to the array
    });
    console.log(arr); // will include the ZIP code(s) of selected location(s).
});

jsFiddle示例在这里

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

带有多个标签的JQuery Select Option

多个SELECT FROM multi table和SORT按日期

Angular Material 7 Multi Select-设置所选值

Kendo ui Multi Select使用值删除选定的元素

jQuery ui multi select显示逗号分隔的文本

yadcf-带有select2的multi_select-选项下拉列表不区分大小写

未选择任何值时,React-Select Multi Select更新道具

当multi.js库修改了<select>时,AJAX请求没有刷新<select>

SQL-SELECT带有多个where的AS

将所选值的集合从Select2-Multi DropDownList传递到控制器

如何在react-boostrap multi select上设置选定的值?

jQuery Multi Select Option-检查是否选择了一个选项

带有jQuery 1.7.x的ui-select2(multiple)不选择值

在DocumentDB中具有多个值的SELECT

HTML <select>标记中有多个值

具有大型数据集的 Mat Multi Select 中的性能问题

如何从SELECT(多个)jQuery中获取所有未选择的值。的JavaScript

ORACLE SQL 联接 MULTI SELECT 语句 ORACLE

带有参数的存储过程中的多个 SELECT

带有多个类的cheerio select元素,用空格分隔

使用带有多个输出列的SELECT IN的UPDATE表

使用带有多个GROUP BY的count(*)的SQL SELECT请求

从REST API填充multi select select2选项

如何在加载时从数据库中预选择“ sw-entity-multi-select”组件值

忽略带有联接的select中的表列值

Laravel Multi Insert问题,带有报价

带有multi_match AND bool的ElasticSearch

带有多个 JOIN 的 SELECT 包括带有它自己的 JOIN 的 LISTAGG

带有模糊性的多个字段上的ElasticSearch multi_match查询