gpt4 book ai didi

jquery - 如果选择选项或打开 url,则显示引导模式

转载 作者:行者123 更新时间:2023-12-01 08:35:10 25 4
gpt4 key购买 nike

我正在尝试将 jquery 的两个不同函数合并为一个函数。

第一个功能是当选择下拉列表时,如果“值”是 URL,则跳转到该 URL,但如果下拉列表没有 URL,则检查它是否有可以打开的模式!

$(function() {
// bind change event to select
$modal = $('#LoginRegister');
if ($(this).val() === 'LoginRegister') {
$modal.modal('show');
} else {
$('#designselectedchange').on('change', function() {
var url = $(this).val(); // get selected value
if (url) { // require a URL
window.location = url; // redirect
}
return false;
});
}
});
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<select title="Select Design" class="selectpicker" id="designselectedchange" name="design">
<option value="/frequently-asked-questions/index.html?question=client-id-register-login-view-progress-previous-order-quotations">pop up</option>
<option value="LoginRegister">Login modal</option>
</select>

<div class="modal fade" id="LoginRegister" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Login/Register</h4>
</div>
<div class="modal-body">
test
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->

最佳答案

更简单的解决方案 - 测试第一个字符是否为 ID:

$(function() {
$('#designselectedchange').on('change', function() {
var val = $(this).val();
if (val) {
if (val.indexOf("#") === 0) $(val).modal('show');
else window.location = val;
};
});
});
<select title="Select Design" class="selectpicker" id="designselectedchange" name="design">
<option value="/frequently-asked-questions/index.html">pop up</option>
<option value="#LoginRegister">Login modal</option>
</select>

关于jquery - 如果选择选项或打开 url,则显示引导模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57131427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com