简体   繁体   English

无线电隐藏。 改为显示div类。 单击div选择链接的收音机

[英]radio hide. show div class instead. on click of div select linked radio

trying to get this working. 试图使这个工作。 Instead of showing the normal radio, or replacing the radio with an image. 而不是显示普通收音机,或用图像替换收音机。 i would like to replace it with a div i can dress up as a button and on hover the class changes and on click the class changes. 我想用div代替它,我可以打扮成一个按钮,将鼠标悬停在类更改上,然后单击类更改。 but also on click the hidden radio is selected. 而且还单击隐藏的单选按钮。 but also require the label (label in reference to the text showing eg as below .co.uk .com .net etc.) to be inside the div/button. 但还要求标签(例如参考下面显示的文本的标签。co.uk.com .net等)位于div / button内。 or be able to add text to the div button. 或能够向div按钮添加文本。 is this possible? 这可能吗?

$(function() {
    $("input [name='domain_ext']").each(function() {
        if ($(this).prop('checked')) {
            $(this).hide();
            $(this).after($("< class='radioButtonOff' />"));
        } else { 
            $(this).hide();
            $(this).after($("<class='radioButtonOn' />"));
        }
    });

    $("input.radio").click(function() {
        if($(this).attr('src') == 'radiobuttonOn') {
            $(this).attr('src', 'radiobuttonOff');
            $(this).prev().attr('checked', 'false');
        } else { // its not checked, so check it
            $(this).attr('src', 'radioButtonOn');
            $(this).prev().attr('checked', 'true');
        }
    });
});

HTML 的HTML

<table class="domain_ext_ribbon">
  <tr>
    <td><label>
      <input type="radio" name="domain_ext" value="all"  />
      All</label></td>
    <td><label>
      <input type="radio" name="domain_ext" value=".co.uk"  />
      .co.uk</label></td>
    <td><label>
      <input type="radio" name="domain_ext" value=".com" />
      .com</label></td>
  </tr>
  <tr>
    <td><label>
      <input type="radio" name="domain_ext" value=".net"  />
      .net</label></td>
    <td><label>
      <input type="radio" name="domain_ext" value=".org"  />
      .net</label></td>
    <td><label>
      <input type="radio" name="domain_ext" value=".biz" />
      .net</label></td> 
  </tr>
</table>

css 的CSS

.radioButtonOff { width: 60px; height: 20px; background: #000000;}
.radioButtonHover { width: 60px; height: 20px; background: #666666;}
.radioButtonOn { width: 60px; height: 20px; background: #999999;}

It looks like you have a bug in your selector; 看起来您的选择器中有错误; you're missing the opening brace around your attribute filter. 您缺少属性过滤器周围的左括号。 So 所以

$("input name='domain_ext']")

should be 应该

$("input [name='domain_ext']")

Also, unless you're using specifically version 1.6 of jQuery, your code should work, but, 另外,除非您使用的是jQuery 1.6版,否则您的代码应该可以运行,但是,

if ($(this).prop('checked'))

is the preferred way to check to see if a radio button is checked, as opposed to 是检查单选按钮是否被选中的首选方法,而不是

if ($(this).attr('checked'))

See this link for more information. 有关更多信息,请参见此链接


And of course this 当然这

$(this).after($("<class='radioButtonOn' />"));

doesn't really make much sense at all. 根本没有任何意义。 I think you meant: 我想你的意思是:

$(this).after($("<div class='radioButtonOff' />"));

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

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