2 回答
TA贡献1802条经验 获得超6个赞
由于无法使用 dataattributes,您可以创建一个保存数据的数组或对象,并检查所选值是否作为该数组中的键存在。
var infoModalData = {
opt1: {
title: 'Opt 1 Title',
text: 'Opt 1 Text',
show: true
},
opt2: {
title: 'Opt 2 Title',
text: 'Opt 2 Text',
show: true
},
opt3: {
title: 'Opt 3 Title',
text: 'Opt 3 Text',
show: true
},
};
$('select').on('change', function() {
let key = $(this).val();
if(!(key in infoModalData) || !infoModalData[key].show) {
// this value has no data
return false;
}
let $modal = $( '#myModal' );
$modal.find('.modal-title').text( infoModalData[key].title );
$modal.find('.modal-body').text( infoModalData[key].title );
$modal.modal('show');
infoModalData[key].
});
var infoModalData = {
opt1: {
title: 'Opt 1 Title',
text: 'Opt 1 Text',
show: true
},
opt2: {
title: 'Opt 2 Title',
text: 'Opt 2 Text',
show: true
},
opt3: {
title: 'Opt 3 Title',
text: 'Opt 3 Text',
show: true
},
};
$('select').on('change', function() {
let key = $(this).val();
if(!(key in infoModalData) || !infoModalData[key].show) {
// this value has no data
return false;
}
let $modal = $( '#myModal' );
$modal.find('.modal-title').text( infoModalData[key].title );
$modal.find('.modal-body').text( infoModalData[key].title );
$modal.modal('show');
infoModalData[key].show = false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.bundle.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="modal" tabindex="-1" role="dialog" id="myModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<select class="form-control">
<option value="" disabled selected>please select an option</option>
<option value="opt1">First opt</option>
<option value="opt2">Second opt</option>
<option value="opt3">Third opt</option>
<option value>No Modal</option>
<option value>No Modal either</option>
</select>
TA贡献1784条经验 获得超2个赞
感谢您的帮助。我能够通过在我的逻辑中添加一些额外的条件来解决这个问题。由于在用户选择选项之前选择框的默认值为 ,因此我能够创建一些逻辑,例如:value=""
if (orientation.value === "flipped-co" && switchType.value === "" && potValue.value === "")
这做了我想要的。再次感谢!
添加回答
举报