简体   繁体   English

如何使用jQuery为Javascript中的两个按钮之间的切换创建有效的代码?

[英]How do I create efficient code for a toggle between two buttons in Javascript using jQuery?

I'm looking for a way to toggle between two buttons efficiently using javascript and jQuery. 我正在寻找一种方法来有效地使用javascript和jQuery在两个按钮之间切换。

Scope 范围

When clicking on either Yes or No, the opposite button will get a disabled CSS class while the clicked button will get an active CSS class. 单击“是”或“否”时,相反的按钮将获得disabled CSS类,而单击的按钮将获得active CSS类。 A var will also be saved with a true false value that will be used later. 一个var也将被保存为true false稍后将使用的值。

html HTML

<div id="buttons">
  <button id="yes">Yes</button>
  <button id="no">No</button>
</div>

js JS

function bindButtons(){
  var buttons = $('#buttons button');

  buttons.on('click', function(e){
    var $this = $(this);
    buttons.removeClass('selected');
    if($this.attr('id') == 'yes'){
      var el = $('#no'),
          val = true;
      $this.removeClass('disabled');
      $this.addClass('selected');
      el.addClass('disabled');
    }
    if($this.attr('id') == 'no'){
      var el = $('#yes'),
          val = false;
      $this.removeClass('disabled');
      $this.addClass('selected');
      el.addClass('disabled');
    }
    //do something with val
  })
}
bindButtons();

jsFiddle 的jsfiddle

http://jsfiddle.net/RobertSheaO/Tjngw/2/ http://jsfiddle.net/RobertSheaO/Tjngw/2/

This should be OK as a replacement to your bindButtons function meat. 这应该可以替代你的bindButtons函数肉。

EDIT 编辑

Apparently, this should also work with more than one button. 显然,这也适用于多个按钮。 Late night coding as well. 深夜编码也是如此。 > _ > > _ >

var buttons = $('#buttons button').on('click', function (e) {

    var $this = $(this).removeClass('disabled').addClass('selected'),
        el = buttons.not(this).addClass('disabled'),
        isYes = $this.is('#yes')
        ;

    // do something with isYes

});

jsFiddle 的jsfiddle

It's perfectly readable for me, but it might not be for you, so this might be better if you'd like: 它对我来说是完全可读的,但它可能不适合你,所以如果你愿意,这可能会更好:

var buttons = $('#buttons button').on('click', function (e) {

    var $this = $(this),
        el = buttons.not(this),
        isYes = $this.is('#yes')
        ;

    $this.removeClass('disabled');
    $this.addClass('selected');
    el.addClass('disabled');

});
function bindButtons(){
  var buttons = $('#buttons button');

  buttons.on('click', function(e){
    var $this = $(this);
    buttons.removeClass('selected').addClass('disabled');
    $this.addClass('selected').removeClass('disabled');

    switch ($this.attr('id')){
        case 'yes': 
            val = true;
            break;
        case 'no': 
            val = false;
            break;
    }
    //do something with val
  })
}
function bindButtons(){
  $('#buttons button').click(function(e){ 
    var thisObj = $(this);
    $('#buttons').removeClass('selected');
    thisObj.removeClass('disabled').addClass('selected').siblings().addClass('disabled');
    var val = thisObj.attr('id')=='yes' ? true : false;    
    //do something with val
  })
}
bindButtons();

http://jsfiddle.net/Tjngw/6/ http://jsfiddle.net/Tjngw/6/

I suggest using the html5 data- attribute in your html for the answer and some jquery toggeling. 我建议在你的html中使用html5 data-属性作为答案和一些jquery toggeling。

html HTML

<div id="buttons">
    <button id="yes" data-answer="true">Yes</button>
    <button id="no" data-answer="false">No</button>
</div>

Toggeling is easy using jQuery: 使用jQuery很容易使用Toggeling:

Javascript 使用Javascript

// Bind events to your buttons
var bindButtons = function(){

    $('#buttons').on('click', 'button', function( e ){

        e.preventDefault();

        $(this)
            .addClass('selected');
            .siblings()
            .removeClass('selected')
            .addClass('disabled');
    });

};

// Init binding
bindButtons();

// Get your answer
var answer = $('#buttons .selected').data('answer');

Demo 演示

Try it on jsbin . 在jsbin上尝试一下

You can do things like shortening: 你可以做缩短的事情:

$this.removeClass('disabled');
$this.addClass('selected');

to: 至:

$this.removeClass('disabled').addClass('selected');

And probably change the second if(){ to an else{ 并且可能将第二个if(){更改为else{

Apart from shortenings like that, unless this code is dragging the browser down (which is isn't) then there's nothing wrong with it. 除了这样的缩短,除非这个代码拖动浏览器(这不是),所以它没有任何问题。

Hopefully this helps a little. 希望这有点帮助。 I usually use jQuery's .not() with $(this). 我通常使用jQuery的.not()$(this).

$('#buttons button').click(function() {
    $('#buttons button').not($(this)).removeClass('enabled').addClass('disabled');
    $(this).removeClass('disabled').addClass('enabled');

    var selected = $(this).attr('id'); // To use the id, or to use text use:
    var selected = $(this).text();  // No need for ids, grabs the text of element
});

You could try something like this: 你可以尝试这样的事情:

function bindButtons(){
  var buttons = $('#buttons button');

  buttons.on('click', function(e){
    var $this = $(this);
    var el = $this;
    var val = $this.attr('id') === 'yes';
    buttons.removeClass('selected disabled');
    $this.addClass('selected')
      .siblings('button').addClass('disabled');

    //do something with val
  })
}

It at least removes the if statement. 它至少删除了if语句。

Try this: 试试这个:

$("#buttons button").on('click', function(e) {
    $(".selected").removeClass('selected');

    var el;

    if($this.attr('id') == 'yes'){
        val = true;

        el = $("#no");
    } else {
        val = false;

        el = $("#yes");
    }

    $this.removeClass('disabled').addClass('selected');
    el.addClass('disabled');
});

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

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