简体   繁体   English

单选按钮作为表格单元格

[英]radio button as table cell

Js Fiddle Js小提琴

here is the JS i have :这是我的 JS:

    $( function() {
  $('td').click( function() {
    $(this).toggleClass("red-cell");
  } );
} );    

and the style :和风格:

td { background: white; cursor: pointer; padding: 2px }
td.red-cell {
    background: #339933; 
}

Easiest to explain by looking at the jsfiddle.通过查看 jsfiddle 最容易解释。 I want to be able to click the table cell and if its green then the radio button "yes" is selected, if clicked again it should toggle to the no radio button.我希望能够单击表格单元格,如果它是绿色的,那么单选按钮“是”被选中,如果再次单击它应该切换到否单选按钮。 I have the radio buttons on the dog picture currently.我目前在狗图片上有单选按钮。 Once its working i would change the radio buttons to be hidden... The fiddle above is the closest i can get with my limited knowledge of js.一旦它工作,我会将单选按钮更改为隐藏......上面的小提琴是我对js的有限知识所能得到的最接近的。 Any suggestions on how i can achieve this?关于我如何实现这一目标的任何建议?

Unnecessarily tricky using toggle (and less clear when reviewing code weeks/months down the road).使用切换不必要的棘手(并且在审查代码周/月时不太清楚)。 Better to explicitly specify what you want done.最好明确指定你想要做什么。

jsFiddle Demo jsFiddle 演示

$('td').click( function() {
   var $this = $(this);
   if ( $this.hasClass('red-cell') ){
      $this.removeClass('red-cell');
      $('#radNo').prop("checked", true);
   }else{
      $this.addClass('red-cell');
      $('#radYes').prop("checked", true);
   }
} );

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

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