如何使用Javascript关闭Bootstrap (5.2)模态框?
P粉369196603
P粉369196603 2024-02-21 14:15:22
0
2
305

为了学校项目的缘故,我使用html中的bootstrap创建了一个模态,其中有一个提示,我必须从javascript检查,如何从javascript关闭模态,以便在提示有效的情况下我可以只保存更改,如果不是,我会抛出异常吗? 小注释(请不要使用 jQuery,我见过一个类似的线程,它使用这个库作为回复,禁止分配)

这是我的 html 代码:

<div class="modal fade" id="bidModal" tabindex="-1" aria-labelledby="bidModal" aria-hidden="true">
        <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="bidModalLabel">Bid amount</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p class="text" id="prompted">How much do you want to bet?</p>
                <div class="input-group mb-2">
                    <input id="bAmount" type="text" class="form-control text" aria-label="Amount of bet">
                    <span class="input-group-text">€</span>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Cancel bid</button>
                <button type="button" onClick="bid()" class="btn btn-success">Save bid</button>
            </div>
        </div>
        </div>
    </div>

这是我的 JavaScript 代码:

function bid(){
    let valueOfBid = document.getElementById("bAmount").value;
    if(isNaN(valueOfBid)){
        //Cancel the prompt
    }

    players[realPlayer].bet=valueOfBid;
    changeButtonState(false);
    theTimer();
}

P粉369196603
P粉369196603

全部回复(2)
P粉733166744

您可以将其添加到应关闭模式的元素中...

data-bs-dismiss="modal"

以下模式根据搜索词和用户匹配生成链接列表。

当他们单击其中一个路由器链接时,由于该行,它将关闭模式

{{ user }}

上面的代码是在模态中生成的 -> 这是整个事情

P粉012875927

请尝试这样。我建议您在将我的代码添加到代码库之前将 isNaN(valueOfBid) 更改为 valueOfBid == ""

function bid(){
    let valueOfBid = document.getElementById("bAmount").value;
    if(valueOfBid == ""){
      alert(1)
        //Cancel the prompt
      var myModalEl = document.getElementById('bidModal');
var modal = bootstrap.Modal.getInstance(myModalEl)
modal.hide();
    }

    // players[realPlayer].bet=valueOfBid;
    // changeButtonState(false);
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!