Javascript 在下拉列表中更改选定的选项值
我的下拉列表看起来像Javascript 在下拉列表中更改选定的选项值,javascript,jquery,html,Javascript,Jquery,Html,我的下拉列表看起来像 <select name="speed" id="ddlCustomer" class="form-control select-basic"> <optgroup label="CustomerId OrderDate SupplyDate Su
<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>
像这样尝试。您已经使用了文本值为selected选项的正则表达式。
var-ddlCustomer=document.querySelector('ddlCustomer');
ddlCustomer.addEventListener('change',function()){
text=$(this).find(“选项:已选”).text();
var值=text.match(/[0-9]+/);
$(此).find(“选项:选定”).text(值);
警报(值);
});代码>
1011 2015-12-18 2015-12-22美国广播公司
1034 2015-12-23 2015-12-28 XYZ
像这样尝试。您已经将regex与所选选项的文本值一起使用。
var-ddlCustomer=document.querySelector('ddlCustomer');
ddlCustomer.addEventListener('change',function()){
text=$(this).find(“选项:已选”).text();
var值=text.match(/[0-9]+/);
$(此).find(“选项:选定”).text(值);
警报(值);
});代码>
1011 2015-12-18 2015-12-22美国广播公司
1034 2015-12-23 2015-12-28 XYZ
var states=[];
var customSelect=$(“#ddlCustomer”);
var customSelectOptions=customSelect.children().children();
//获取每个状态,然后将它们推送到数组中
//初次国家声明
customSelectOptions.each(函数(){
var state=$(this.text();
states.push({state:state});
如果($(this).is(':selected')){
$(this).text($(this.attr('value')).split('uu')[0]);
}
});
//在焦点上,始终保留完整的州名称
customSelect.on('focus',function(){
customSelectOptions.each(函数(索引){
$(this).text(states[index].state);
});
//更改时,将值附加到选定选项
$(this).on('change',function()){
自定义选择选项。每个(功能)选项{
如果($(this).is(':selected')){
$(this).text($(this.attr('value')).split('uu')[0]);
}
});
//取消焦点选择完成
$(this.blur();
});
});代码>
1011 2015-12-18 2015-12-22美国广播公司
1034 2015-12-23 2015-12-28 XYZ
var states=[];
var customSelect=$(“#ddlCustomer”);
var customSelectOptions=customSelect.children().children();
//获取每个状态,然后将它们推送到数组中
//初次国家声明
customSelectOptions.each(函数(){
var state=$(this.text();
states.push({state:state});
如果($(this).is(':selected')){
$(this).text($(this.attr('value')).split('uu')[0]);
}
});
//在焦点上,始终保留完整的州名称
customSelect.on('focus',function(){
customSelectOptions.each(函数(索引){
$(this).text(states[index].state);
});
//更改时,将值附加到选定选项
$(this).on('change',function()){
自定义选择选项。每个(功能)选项{
如果($(this).is(':selected')){
$(this).text($(this.attr('value')).split('uu')[0]);
}
});
//取消焦点选择完成
$(this.blur();
});
});代码>
1011 2015-12-18 2015-12-22美国广播公司
1034 2015-12-23 2015-12-28 XYZ
您想在哪里添加客户ID?我想通过下拉菜单显示CustomerID,而不是所有详细信息。您可以通过选择组上的onchange功能更改它,并将“this”传递给用户。但是您需要将旧值保存在一个var中,这样,如果用户选择了不同的内容,旧值将返回。数据从何而来?因此,您希望添加1101作为下拉选项,而不是“1011 2015-12-18 2015-12-22 ABC”?使用alert($('.select basic option:selected').text().split(“”[0])代码>要在何处添加客户ID?我希望下拉列表显示CustomerID,而不是所有详细信息。您可以通过选择组上的onchange函数更改它,并将“this”传递给。但是您需要将旧值保存在一个var中,这样,如果用户选择了不同的内容,旧值将返回。数据从何而来?因此,您希望添加1101作为下拉选项,而不是“1011 2015-12-18 2015-12-22 ABC”?使用alert($('.select basic option:selected').text().split(“”[0])代码>右侧警报值具有修剪文本,但下拉列表中的实际值应设置为this清除选项值,这不是所需的警报值具有修剪文本,但下拉列表中的实际值应设置为this清除选项值,这只是一个小问题。如果用户打开下拉列表,但没有选择任何内容,则下拉列表中的文本不是“1011 2015-12-18 2015-12-22 ABC”,但这一切正常。单击“打开”下拉列表,然后在表单中单击“退出”,值为“更改”,使其正常工作。请检查有问题的更新脚本。欢迎提出任何改进建议。谢谢这只是个小问题。如果用户打开下拉列表,但没有选择任何内容,则下拉列表中的文本不是“1011 2015-12-18 2015-12-22 ABC”,但这一切正常。单击“打开”下拉列表,然后在表单中单击“退出”,值为“更改”,使其正常工作。请检查有问题的更新脚本。欢迎提出任何改进建议。谢谢
customSelect.focusout(function () {
customSelectOptions.each(function (options) {
if ($(this).is(':selected')) {
$(this).text($(this).attr('value').split('_')[0]);
$(this).blur();
}
});
});
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;
}
};