gpt4 book ai didi

javascript - 仅当选中两个选项之一并按按钮提交时,如何启动 Bootstrap 模式

转载 作者:行者123 更新时间:2023-12-03 04:32:59 28 4
gpt4 key购买 nike

我有来自 http://getbootstrap.com/javascript/ 的标准 Bootstrap 模式

HTML 代码:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" 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" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

现在上面的模式可以工作了,因为单击按钮后就会弹出。

但是我有两个单选框,我只想在用户选中accept单选选项并点击按钮后打开模式。

<form>
<div class="radio">
<label><input type="radio" name="xx" checked>Accept</label>
</div>
<div class="radio">
<label><input type="radio" name="xx">Reject</label>
</div>

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
</form>

上述代码的问题是,即使用户选中了reject选项,模式也会弹出。我不希望这种事发生。

最佳答案

您可能希望返回到按程序触发模式,以向模式的显示添加条件。

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" 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" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

<form>
<div class="radio">
<label>
<input type="radio" name="xx" value=true checked>Accept</label>
</div>
<div class="radio">
<label>
<input type="radio" name="xx" value=false>Reject</label>
</div>

<!-- Drop the attributes that trigger the modal display -->
<button type="button" class="btn btn-primary btn-lg conditional-launch">Conditional Modal</button>
</form>

<script>
// Add this in to target the button and bind to the click event
$(".conditional-launch").click(
function(event) {

// Then get the currently selected radio button's value
var value = $("input[name=xx]:checked").val();

// Check the value to make sure you want to show the modal
if (value === 'true') {
$("#myModal").modal('show');
}
}
);

</script>

这里有一个 jsfiddle 来检查它的工作情况:https://jsfiddle.net/z7b5krse/ 。只要在 script block 之前加载 jQuery 和 Bootstrap,就可以复制/粘贴此处的代码。

关于javascript - 仅当选中两个选项之一并按按钮提交时,如何启动 Bootstrap 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43412375/

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