Javascript 根据另一个下拉列表的选项值设置一个下拉列表的文本
我有一个复选框将时间设置为24小时或12小时格式。如果用户选中或取消选中此选项,我希望当前时间以备用时间格式设置为相同时间。因此,如果当前时间为17:00,则应设置为下午5:00。目前,它只是将其设置回第一个选项值。这是最新的Javascript 根据另一个下拉列表的选项值设置一个下拉列表的文本,javascript,jquery,Javascript,Jquery,我有一个复选框将时间设置为24小时或12小时格式。如果用户选中或取消选中此选项,我希望当前时间以备用时间格式设置为相同时间。因此,如果当前时间为17:00,则应设置为下午5:00。目前,它只是将其设置回第一个选项值。这是最新的 24小时 变量a=['12.00'、'13.00'、'14.00'、'15.00'、'16.00'、'17.00']; 变量b=['12.00'、'1.00pm'、'2.00pm'、'3.00pm'、'4.00pm'、'5.00pm']; var d=[]; $('#is
24小时
变量a=['12.00'、'13.00'、'14.00'、'15.00'、'16.00'、'17.00'];
变量b=['12.00'、'1.00pm'、'2.00pm'、'3.00pm'、'4.00pm'、'5.00pm'];
var d=[];
$('#isCheckedFormat')。单击(函数(){
currenttime=$(“#time1”).val();
console.log(当前时间);
如果($('isCheckedFormat')。是(':checked')){
d=a;
c=1;
$(“#time1选项”).remove();
对于(var i=0;i这样的东西应该可以做到这一点
var a = ['12.00','13.00','14.00','15.00','16.00','17.00'];
var b = ['12.00','1.00pm','2.00pm','3.00pm','4.00pm','5.00pm'];
function populate(e) {
var val = $('#time1').find('option:selected').index(),
sel = $('#time1').empty();
$.each(e.target.checked ? b : a, function(i, time) {
$('<option />', {
value : time,
text : time,
prop : {
selected : i === val
}
}).appendTo(sel);
});
}
$('#isCheckedFormat').on('change', populate).trigger('change')
var a=['12.00'、'13.00'、'14.00'、'15.00'、'16.00'、'17.00'];
变量b=['12.00'、'1.00pm'、'2.00pm'、'3.00pm'、'4.00pm'、'5.00pm'];
函数填充(e){
var val=$('#time1').find('option:selected').index(),
sel=$('#time1').empty();
$。每个(e.target.checked?b:a,函数(i,时间){
$('', {
价值:时间,
文本:时间,
道具:{
所选:i==val
}
}).附录(sel);
});
}
$('isCheckedFormat')。on('change',populate)。trigger('change'))
您正在获取选项的值,尝试重置选项等。但是您在更改选项文本时更改了值,因此不匹配。我决定改为使用索引,这是一致的。注意:这只是一个快速修复,您的设置整体上似乎有点不方便
var a = ['12.00','13.00','14.00','15.00','16.00','17.00'];
var b = ['12.00','1.00pm','2.00pm','3.00pm','4.00pm','5.00pm'];
var d = [];
$('#isCheckedFormat').click(function() {
currenttime = $("#time1").val();
element = $("#time1 option:selected").index();
console.log(currenttime);
if($('#isCheckedFormat').is(':checked')){
d = a;
c = 1;
$('#time1 option').remove();
for(var i = 0; i<d.length; i++) {
$('#time1').append('<option val="'+ c +'">'+d[i]+'</option>');
c++;
}
$('#time1 option:eq('+element+')').prop('selected',true);
}else{
d = b;
c = 1;
$('#time1 option').remove();
for(var i = 0; i<d.length; i++) {
$('#time1').append('<option val="'+ c +'">'+d[i]+'</option>');
c++;
}
$('#time1 option:eq('+element+')').prop('selected',true);
};
});
var a=['12.00'、'13.00'、'14.00'、'15.00'、'16.00'、'17.00'];
变量b=['12.00'、'1.00pm'、'2.00pm'、'3.00pm'、'4.00pm'、'5.00pm'];
var d=[];
$('#isCheckedFormat')。单击(函数(){
currenttime=$(“#time1”).val();
元素=$(“#时间1选项:选定”).index();
console.log(当前时间);
如果($('isCheckedFormat')。是(':checked')){
d=a;
c=1;
$(“#time1选项”).remove();
对于(var i=0;i仅添加两行:
1) 一行保存当前选定值的索引:
var x = $('#time1').find('option:selected').index();
2) 一行设置为使用保存的索引设置所选值:
$('#time1').val(d[x]);
代码如下所示:
...
$('#isCheckedFormat').click(function() {
...
console.log(currenttime);
var x = $('#time1').find('option:selected').index(); // ADDED
if($('#isCheckedFormat').is(':checked')){
...
}else{
...
for(var i = 0; i<d.length; i++) {
$('#time1').append('<option val="'+ c +'">'+d[i]+'</option>');
$('#time1').val(d[x]); // ADDED
c++;
}
...
};
});
。。。
$('#isCheckedFormat')。单击(函数(){
...
console.log(当前时间);
var x=$('#time1')。查找('option:selected')。索引();//已添加
如果($('isCheckedFormat')。是(':checked')){
...
}否则{
...
对于(var i=0;iu应该考虑使用),因为它可以变得非常适合以数组形式管理时间。尝试使用<代码> $。
...
$('#isCheckedFormat').click(function() {
...
console.log(currenttime);
var x = $('#time1').find('option:selected').index(); // ADDED
if($('#isCheckedFormat').is(':checked')){
...
}else{
...
for(var i = 0; i<d.length; i++) {
$('#time1').append('<option val="'+ c +'">'+d[i]+'</option>');
$('#time1').val(d[x]); // ADDED
c++;
}
...
};
});