是否可以从刚刚从该列表中删除的项目中获取 select 列表的名称
Is it possible to get the name of a select list from an item that has just been removed from that list
我试图在从 selectize 列表中删除某项后更新数组中的值。为此,我需要列表的 attr('name')
。如果出现 item_remove
,$item
可以获得 select 列表,但找不到我需要的 attr('name')
。
下面的解决方案是我如何为 item_add
事件执行此操作。您会看到 var thisSelect
获取 select 列表,从中,我可以用实际列表值填充值数组。
/**
* Add callback behaviour once a select element has been selected.
*/
$('[data-type="select"]').each(function(i, v) {
v.selectize.on('item_add', function (value, $item) {
var thisSelect = $item.parents('.controls').find('select'),
name = thisSelect.attr('name'),
values = [],
exclusiveSelected = thisSelect.find('option[data-exclusive="true"]:selected');
if (exclusiveSelected.length) {
values.push({
"value": exclusiveSelected.val(),
"label": exclusiveSelected.text().trim(),
"label-key": exclusiveSelected.attr("label-key")
});
thisSelect.val(exclusiveSelected.val()).trigger('change');
} else {
thisSelect.siblings('.selectize-control').find('.item').each(function(i, v) {
values.push({
"value": $(v).data('value'),
"label": $(v).text().trim(),
"label-key": $(v).attr("label-key")
});
});
}
setMultipleFieldValues(name, values);
validate(documentId, name, fields);
});
});
以下是我当前针对 item_remove
事件的解决方案。控制台中出现 Cannot set property 'values' of undefined
错误,因为由于返回的列表名称未定义而无法填充值数组。
/**
* Add callback behaviour once a select element has been unselected.
*/
$('[data-type="select"]').each(function(i, v) {
v.selectize.on('item_remove', function (value, $item) {
var thisSelect = $item.parents('.controls').find('select'),
name = thisSelect.attr('name'),
values = [];
thisSelect.siblings('.selectize-control').find('.item').each(function(i, v) {
values.push({
"value": $(v).data('value'),
"label": $(v).text().trim(),
"label-key": $(v).attr("label-key")
});
});
setMultipleFieldValues(name, values);
validate(documentId, name, fields);
});
});
附件是 select 元素,我试图从
获取名称 "additionalWork"
您需要在对象被删除时获取它。我建议将函数解析为 onDelete
回调
演示 http://jsfiddle.net/uZmcD/91/
$(function() {
$('#select-name').selectize({
plugins: ['remove_button'],
onDelete: function(values) {
item_remove_function($(this));
}
});
});
function item_remove_function($this) {
var name = $this[0].$wrapper.siblings('select').attr('name');
alert('select name = ' + name);
}
感谢您的回复,我想出的解决方案涉及获取 $input 而不是 $item。为此,我可以获得列表中的值。
/**
* Add callback behaviour once a select element has been unselected.
*/
$('[data-type="select"]').each(function(i, v) {
v.selectize.on('item_remove', function (value, $item) {
var thisSelect = this.$input,
name = thisSelect.attr('name'),
values = [],
exclusiveSelected = thisSelect.find('option[data-exclusive="true"]:selected');
if (exclusiveSelected.length) {
values.push({
"value": exclusiveSelected.val(),
"label": exclusiveSelected.text().trim(),
"label-key": exclusiveSelected.attr("label-key")
});
thisSelect.val(exclusiveSelected.val()).trigger('change');
} else {
thisSelect.siblings('.selectize-control').find('.item').each(function(i, v) {
values.push({
"value": $(v).data('value'),
"label": $(v).text().trim(),
"label-key": $(v).attr("label-key")
});
});
}
setMultipleFieldValues(name, values);
validate(documentId, name, fields);
});
});
我试图在从 selectize 列表中删除某项后更新数组中的值。为此,我需要列表的 attr('name')
。如果出现 item_remove
,$item
可以获得 select 列表,但找不到我需要的 attr('name')
。
下面的解决方案是我如何为 item_add
事件执行此操作。您会看到 var thisSelect
获取 select 列表,从中,我可以用实际列表值填充值数组。
/**
* Add callback behaviour once a select element has been selected.
*/
$('[data-type="select"]').each(function(i, v) {
v.selectize.on('item_add', function (value, $item) {
var thisSelect = $item.parents('.controls').find('select'),
name = thisSelect.attr('name'),
values = [],
exclusiveSelected = thisSelect.find('option[data-exclusive="true"]:selected');
if (exclusiveSelected.length) {
values.push({
"value": exclusiveSelected.val(),
"label": exclusiveSelected.text().trim(),
"label-key": exclusiveSelected.attr("label-key")
});
thisSelect.val(exclusiveSelected.val()).trigger('change');
} else {
thisSelect.siblings('.selectize-control').find('.item').each(function(i, v) {
values.push({
"value": $(v).data('value'),
"label": $(v).text().trim(),
"label-key": $(v).attr("label-key")
});
});
}
setMultipleFieldValues(name, values);
validate(documentId, name, fields);
});
});
以下是我当前针对 item_remove
事件的解决方案。控制台中出现 Cannot set property 'values' of undefined
错误,因为由于返回的列表名称未定义而无法填充值数组。
/**
* Add callback behaviour once a select element has been unselected.
*/
$('[data-type="select"]').each(function(i, v) {
v.selectize.on('item_remove', function (value, $item) {
var thisSelect = $item.parents('.controls').find('select'),
name = thisSelect.attr('name'),
values = [];
thisSelect.siblings('.selectize-control').find('.item').each(function(i, v) {
values.push({
"value": $(v).data('value'),
"label": $(v).text().trim(),
"label-key": $(v).attr("label-key")
});
});
setMultipleFieldValues(name, values);
validate(documentId, name, fields);
});
});
附件是 select 元素,我试图从
获取名称 "additionalWork"您需要在对象被删除时获取它。我建议将函数解析为 onDelete
回调
演示 http://jsfiddle.net/uZmcD/91/
$(function() {
$('#select-name').selectize({
plugins: ['remove_button'],
onDelete: function(values) {
item_remove_function($(this));
}
});
});
function item_remove_function($this) {
var name = $this[0].$wrapper.siblings('select').attr('name');
alert('select name = ' + name);
}
感谢您的回复,我想出的解决方案涉及获取 $input 而不是 $item。为此,我可以获得列表中的值。
/**
* Add callback behaviour once a select element has been unselected.
*/
$('[data-type="select"]').each(function(i, v) {
v.selectize.on('item_remove', function (value, $item) {
var thisSelect = this.$input,
name = thisSelect.attr('name'),
values = [],
exclusiveSelected = thisSelect.find('option[data-exclusive="true"]:selected');
if (exclusiveSelected.length) {
values.push({
"value": exclusiveSelected.val(),
"label": exclusiveSelected.text().trim(),
"label-key": exclusiveSelected.attr("label-key")
});
thisSelect.val(exclusiveSelected.val()).trigger('change');
} else {
thisSelect.siblings('.selectize-control').find('.item').each(function(i, v) {
values.push({
"value": $(v).data('value'),
"label": $(v).text().trim(),
"label-key": $(v).attr("label-key")
});
});
}
setMultipleFieldValues(name, values);
validate(documentId, name, fields);
});
});