简体   繁体   English

如何使用引导程序在单击同一页面上的另一个按钮时保持选择的按钮?

[英]How to keep selected the button on click of another button on same page using bootstrap?

On click of one of blue buttons(only one can be selected at a time) it is selected. 单击一个蓝色按钮(一次只能选择一个)时,它被选中。 But when I click on Buy button the previous blue button gets unselected. 但是,当我单击“ Buy按钮时,先前的蓝色按钮未被选中。 Therefore, I cannot read the value of blue button by onclick of yellow button. 因此,我不能读蓝色按钮的值onclick黄色按钮。

Fiddle here . 在这里摆弄。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Sign in Success</title>
</head>
<body>
    <div class="btn-group" style="background:black;width:100%">
        <button type="button" class="btn btn-default">Tr</button>
        <div style="width:10px"></div>
        <button type="button" class="btn btn-default">T</button>
        <div style="width:10px"></div>
        <button type="button" class="btn btn-default">Vt</button>
        <div style="width:10px"></div>
        <button type="button" class="btn btn-default">Rt</button>
    </div>
    <br> <br>
    <h2 style="text-decoration:underline;" align="center">T</h2>
    <div align="center"><!-- id="centerit" -->
        <br> <br> <br>
        <!-- <div style="width:50px"></div> -->
        <button type="button" class="btn btn-primary cust" value="bu">Butter</button>
        <button type="button" class="btn btn-primary cust" value="mi">Milk</button>
        <button type="button" class="btn btn-primary cust" value="bi">Biscuit</button>
        <br> <br>
        <button type="button" class="btn btn-warning" value = "submit">Buy!</button>
    </div>
</body>
</html>

I expect the blue button to remain selected when i click on buy button which is not happening now. 我希望当我单击现在不发生的buy button时, blue button将保持选中状态。

You can put your buttons in a form and replace the three blue buttons with radio buttons. 您可以将按钮放入表单中,并用单选按钮替换三个蓝色按钮。 See the documentation here: https://getbootstrap.com/docs/4.0/components/buttons/#checkbox-and-radio-buttons 请参阅此处的文档: https : //getbootstrap.com/docs/4.0/components/buttons/#checkbox-and-radio-buttons

Updated fiddle: https://jsfiddle.net/0wj1m8ce/ 更新的提琴: https : //jsfiddle.net/0wj1m8ce/

 <div class="btn-group btn-group-toggle"  data-toggle="buttons" >
      <label class="btn btn-primary cust">
        <input type="radio" name="options" id="option1" autocomplete="off" value="bu"> Butter
      </label>
      <label class="btn btn-primary cust">
        <input type="radio" name="options" id="option2" autocomplete="off" value="mi"> Milk
      </label>
      <label  class="btn btn-primary cust">
        <input type="radio" name="options" id="option3" autocomplete="off" value="bi" > Biscuit
      </label>
    </div>

Try adding radio buttons instead of buttons 尝试添加单选按钮而不是按钮

That is what you required 那就是你所需要的

<div id="radioBtn" class="btn-group">
    <a class="btn btn-primary btn-sm active" data-toggle="fun" data-title="bu">Butter</a>
    <a class="btn btn-primary btn-sm notActive" data-toggle="fun" data-title="mi">Milk</a>
    <a class="btn btn-primary btn-sm notActive" data-toggle="fun" data-title="bi">Biscuit</a>
</div>

<script>
    $('#radioBtn a').on('click', function(){
    var sel = $(this).data('title');
    var tog = $(this).data('toggle');
    $('#'+tog).prop('value', sel);

    $('a[data-toggle="'+tog+'"]').not('[data-title="'+sel+'"]').removeClass('active').addClass('notActive');
    $('a[data-toggle="'+tog+'"][data-title="'+sel+'"]').removeClass('notActive').addClass('active');
})
</script>

<style>
#radioBtn .notActive{
    color: #3276b1;
    background-color: #fff;
}
</style>

Here is an updated JSFiddle 这是更新的JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何替换引导程序中同一页面上的按钮单击的div内容 - how to replace div content for button click on same page in bootstrap 如何使用Twitter Bootstrap按钮点击导航到另一个页面? - How do I navigate to another page on button click with Twitter Bootstrap? 刷新jsp页面单击单选按钮后如何保持选中单选按钮 - How to keep radio button selected after refresh the jsp page click in radio button 如果页面刷新,如何保持选中单选按钮 - How to keep the radio button selected if the page is refreshed 如果使用引导程序,如何在单击按钮时将一个选项卡切换到另一个选项卡? - How to switch one tab to another on button click if using bootstrap? 如何使用JavaScript单击按钮将用户重定向到另一个页面? - How to redirect a user to another page with a button click using only JavaScript? 当按钮单击同一页面时,在引导模式中获取 id - Get id in bootstrap modal when button click on same page 单击“提交”按钮后如何保持在同一页面上,并保持我在JavaScript中输入的日期 - How to stay on same page after click “submit” button, and keep the date I entered in JavaScript 如何在 vuetify 中单击按钮导航到另一个页面? - How to navigate to another page on button click in vuetify? 如何在按钮单击时重定向到另一个页面 - How to redirect on button click to another page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM