简体   繁体   English

一次只允许单击一个按钮

[英]Allow only one button to be clicked at a time

I'm creating a form with different button options. 我正在创建一个具有不同按钮选项的表单。 I want the user to only be allowed to select one button. 我希望只允许用户选择一个按钮。 Basically I have 6 of these buttons inside of the form and when you click it the button color changes to green and says selected. 基本上,我在表单中有6个按钮,当您单击它时,按钮的颜色变为绿色,并表示已选中。 Right now I can select all of the button options, I want it to where when I click one of the buttons if I click another one, then the first one I selected gets "unchecked" 现在,我可以选择所有按钮选项,如果要单击另一个按钮,则希望将其放到单击某个按钮的位置,然后我选择的第一个按钮将变为“未选中”

   <button type="button" class="button-school" value="hidden-springs">
              <h2>Hidden Springs</h2>
              <span class="select-active">SELECT</span>
   </button>

I only posted one of the buttons but they are all the same with different values and h2 values. 我只发布了一个按钮,但它们都是相同的,只是值和h2值不同。 I was trying to find something online to help me but I was really only seeing things for radio buttons, this one is one that I made myself. 我试图在网上找到可以帮助我的东西,但实际上我只看到单选按钮的东西,这是我自己制作的。

Here is what I have so far: 这是我到目前为止的内容:

<script>
jQuery('.button-school').click(function() {
  jQuery(this).toggleClass('active');
  if (jQuery(this).hasClass('active')) {
    jQuery(this).html(jQuery(this).html().replace('SELECT', 'SELECTED'));
  } else {
    jQuery(this).html(jQuery(this).html().replace('SELECTED', 'SELECT'));
  }
});

</script>

I see stuff about "if clicked" can I do something where I can only have one "active"? 我看到有关“如果被单击”的信息,我可以做一些只能有一个“活动”的事情吗?

jQuery(function($) {
  $('.button-school').click(function() {
    $('.button-school').not(this).removeClass('active').html(function() {
      return $(this).html().replace('SELECTED', 'SELECT');
    });
    $(this).addClass('active').html(jQuery(this).html().replace('SELECT', 'SELECTED'));
  });
});

JSFiddle: https://jsfiddle.net/TrueBlueAussie/v2ddpuw6/2/ JSFiddle: https ://jsfiddle.net/TrueBlueAussie/v2ddpuw6/2/

Notes: 笔记:

  • jQuery(function(){}) is a shortcut for a DOM ready handler, which also passed itself as the first parameter. jQuery(function(){})是DOM ready处理程序的快捷方式,该处理程序还将自身作为第一个参数传递。 This way you can avoid loads of jQuery and use $ safely. 这样,您可以避免加载jQuery并安全地使用$
  • If you use html() with a function, the return value is used to replace the existing html . 如果将html()与函数一起使用,则返回值将用于替换现有的html
  • You can exclude the current selection from all matches with not() to filter all except the clicked button. 您可以使用not()从所有匹配项中排除当前选择,以过滤除单击按钮之外的所有内容。

You could always use both buttons and radio buttons 您可以同时使用按钮单选按钮

HTML: HTML:

<input type="radio" hidden name="_groupname" value="what-gets-submitted" />
<button type="button" class="cstm-rad" onclick="this.previousElementSibling.checked=true;">Custom Radio Button</button>

CSS selecting, style the button based on whether or not it's preceded by a :checked item: CSS选择按钮的样式,该样式取决于按钮前面是否带有:checked项:
button.cstm-rad{ /*inactive style*/ }
:checked + button.cstm-rad{ /*active style*/ }

EDIT NOTE: This is almost exactly the same solution I submitted originally, just cleaned of superfluous text, more CSS, and given non-terrible naming conventions. 编辑说明:这几乎与我最初提交的解决方案完全相同,只是清除了多余的文本,更多的CSS并给出了不可怕的命名约定。

I have created a really crude way of implementing this, but basically you have it so when one of the submit buttons are clicked it will then hide all of the submit buttons so the user can no longer click on them. 我已经创建了一种非常粗略的实现方式,但是基本上您可以做到这一点,因此,当单击一个提交按钮时,它将隐藏所有提交按钮,因此用户无法再单击它们。

jsFiddle : https://jsfiddle.net/v2ddpuw6/ jsFiddle: https ://jsfiddle.net/v2ddpuw6/

Html HTML

<input type="button" value="Submit1" class="Submit"/>
<input type="button" value="Submit2" class="Submit"/>
<input type="button" value="Submit3" class="Submit"/>
<input type="button" value="Submit4" class="Submit"/>
<input type="button" value="Submit5" class="Submit"/>

jQuery jQuery的

$(function()
{
    $('.Submit').on('click', function()
  {
    $('.Submit').hide();
  });
});

i have check your code and got the issue. 我已经检查了您的代码并得到了问题。 Here is the working example what you are looking for. 这是您正在寻找的工作示例。

Please check here is the code. 请检查这里的代码。

 jQuery('.button-school').click(function() { if (!jQuery(this).hasClass('active')) { jQuery('.button-school').removeClass('active'); jQuery('.button-school').html(jQuery('.button-school').html().replace('SELECTED', 'SELECT')); jQuery(this).toggleClass('active'); jQuery(this).html(jQuery(this).html().replace('SELECT', 'SELECTED')); } else { jQuery(this).toggleClass('active'); jQuery(this).html(jQuery(this).html().replace('SELECTED', 'SELECT')); } }); 
 .active{ background:green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type="button" class="button-school" value="hidden-springs"> <h2>Hidden Springs</h2> <span class="select-active">SELECT</span> </button> <button type="button" class="button-school" value="hidden-springs"> <h2>Hidden Springs</h2> <span class="select-active">SELECT</span> </button> <button type="button" class="button-school" value="hidden-springs"> <h2>Hidden Springs</h2> <span class="select-active">SELECT</span> </button> 

Enjoy !!!!!!!!!!! 请享用 !!!!!!!!!!!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM