[英]Change selected option value in dropdown
My dropdown looks like, 我的下拉菜单看起来像
<select name="speed" id="ddlCustomer" class="form-control select-basic">
<optgroup label="CustomerId OrderDate SupplyDate Supplier">
<option value="1011_2">1011 2015-12-18 2015-12-22 ABC</option>
<option value="1011_2">1034 2015-12-23 2015-12-28 XYZ</option>
</optgroup>
</select>
Currently the dropdown shows options like "1011 2015-12-18 2015-12-22 ABC", that is fine but when user selects an option, I need to show only "CustomerId" ie, 1011 in this case. 当前,下拉列表显示诸如“ 1011 2015-12-18 2015-12-22 ABC”之类的选项,这很好,但是当用户选择一个选项时,在这种情况下,我仅需要显示“ CustomerId”,即1011。
Your help is really appreciated. 非常感谢您的帮助。 Thanks
谢谢
Added script for focusout 添加了用于聚焦的脚本
customSelect.focusout(function () {
customSelectOptions.each(function (options) {
if ($(this).is(':selected')) {
$(this).text($(this).attr('value').split('_')[0]);
$(this).blur();
}
});
});
var states = []; var customSelect = $('#ddlCustomer'); var customSelectOptions = customSelect.children().children(); // Get each state then push them to the array // Initial state declaration customSelectOptions.each(function() { var state = $(this).text(); states.push({ state: state }); if ($(this).is(':selected')) { $(this).text($(this).attr('value').split('_')[0]); } }); // On focus, always retain the full state name customSelect.on('focus', function() { customSelectOptions.each(function(index) { $(this).text(states[index].state); }); // On change, append the value to the selected option $(this).on('change', function() { customSelectOptions.each(function(options) { if ($(this).is(':selected')) { $(this).text($(this).attr('value').split('_')[0]); } }); // Un-focus select on finish $(this).blur(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="speed" id="ddlCustomer" class="form-control select-basic"> <optgroup label="CustomerId OrderDate SupplyDate Supplier"> <option value="1011_2">1011 2015-12-18 2015-12-22 ABC</option> <option value="1034_2">1034 2015-12-23 2015-12-28 XYZ</option> </optgroup> </select>
Try like this.You have use regex with text value of selected option. 尝试这样。您已将正则表达式与选定选项的文本值一起使用。
var ddlCustomer = document.querySelector('#ddlCustomer'); ddlCustomer.addEventListener('change',function(){ text = $(this).find("option:selected").text(); var value = text.match(/[0-9]+/); $(this).find("option:selected").text(value); alert(value); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="speed" id="ddlCustomer" class="form-control select-basic"> <optgroup label="CustomerId OrderDate SupplyDate Supplier"> <option value="1011_2">1011 2015-12-18 2015-12-22 ABC</option> <option value="1011_2">1034 2015-12-23 2015-12-28 XYZ</option> </optgroup> </select>
See fiddle here https://jsfiddle.net/88cozeaz/1/ 在这里看到小提琴https://jsfiddle.net/88cozeaz/1/
document.getElementById('#ddlCustomer').onchange = function() {
var option = this.options[this.selectedIndex];
option.setAttribute('data-text', option.text);
option.text =$(this).val();
// Reset texts for all other but current
for (var i = this.options.length; i--; ) {
if (i == this.selectedIndex) continue;
var text = this.options[i].getAttribute('data-text');
if (text) this.options[i].text = text;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.