jQuery单选按钮,标签,更改和单击事件

时间:2010-12-17 20:12:01

标签: jquery forms click label

我正在尝试开发一个具有多个单选按钮的表单。选择单选按钮将显示与该单选按钮相关的一组选项。单击标签(在这种情况下将是图像)也应该这样做。我有单选按钮部分工作,我的标签有问题。单击标签会选择正确的单选按钮...但不显示该按钮的内容。我知道我可以解决这个问题,就像我解决单选按钮更改事件一样,但我不想引入比我需要的代码更多的代码。话虽如此,我也知道必须有一种更有效的方法来解决单选按钮更改事件,因为它们都做同样的事情,只有价值在变化。

这是我正在使用的精简版本。我正在寻找帮助我正确完成这项工作的人,同样重要的是,我还要解决编码方法效率低下问题。

<!doctype html>
  <html lang="en">
  <head>
  <meta charset="utf-8">
  <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript">
  $(document).ready(function() {
      $('#contain-me .option').hide();
      $('#contain-me .option.option-1').addClass('chosen').show();

      $('.label').click(function() {
          $(this).prev('input[type=radio]:first').attr('checked', 'checked');
      });

      $("#payment-visual-selector :radio[name='pt']").change(function(){
          var newVal = $(":radio[name='pt']:checked").val();       
          if (newVal === "1") {
              $('#contain-me .option').hide();
              $('#contain-me .option.option-1').addClass('chosen').show();
          } else if (newVal === "2") {
              $('#contain-me .option').hide();
              $('#contain-me .option.option-2').addClass('chosen').show();
          } else if (newVal === "3") {
              $('#contain-me .option').hide();
              $('#contain-me .option.option-3').addClass('chosen').show();
          } else if (newVal === "4") {
              $('#contain-me .option').hide();
              $('#contain-me .option.option-4').addClass('chosen').show();
          } else if (newVal === "5") {
              $('#contain-me .option').hide();
              $('#contain-me .option.option-5').addClass('chosen').show();
          } else if (newVal === "6") {
              $('#contain-me .option').hide();
              $('#contain-me .option.option-6').addClass('chosen').show();
          }       
      });
  });
  </script>
  </head>

  <body>

  <h3>Visual Selector</h3>
  <div class="row" id="payment-visual-selector">
    <p><input type="radio" value="1" id="pt-1" name="pt" class="option-1 radio" checked="checked" /><label class="label" for="pt-1"><img src="img/icon-1a.gif" alt="icon-1a" class="option-1" /> <img src="img/icon-1b.gif" alt="icon-1b" class="option-1" /> <img src="img/icon-1c.gif" alt="icon-1c" class="option-1" /> <img src="img/icon-1d.gif" alt="icon-1d" class="option-1" /></label></p>
    <p><input type="radio" value="2" id="pt-2" name="pt" class="option-2 radio" /><label class="label" for="pt-2"><img src="img/icon-2.gif" alt="icon-2" class="option-2" /></label> <a href="#" class="additional-info">Link for Option #2</a></p>
    <p><input type="radio" value="3" id="pt-3" name="pt" class="option-3 radio" /><label class="label" for="pt-3"><img src="img/icon-3.gif" alt="icon-3" class="option-3" /></label> <a href="#" class="additional-info">Link for Option #3</a></p>
    <p><input type="radio" value="4" id="pt-4" name="pt" class="option-4 radio" /><label class="label" for="pt-4"><img src="img/icon-4.gif" alt="icon-4" class="option-4" /></label></p>
    <p><input type="radio" value="5" id="pt-5" name="pt" class="option-5 radio" /><label class="label" for="pt-5"><img src="img/icon-5.gif" alt="icon-5" class="option-5" /></label></p>
    <p><input type="radio" value="6" id="pt-6" name="pt" class="option-6 radio" /><label class="label" for="pt-6"><img src="img/icon-6.gif" alt="icon-6" class="option-6" /></label></p>
    <div class="clear"></div>
  </div>

  <h3>Content Block</h3>
  <div id="contain-me">
    <div class="option option-1">
      Option 1 content goes here
    </div>
    <div class="option option-2">
      Option 2 content goes here
    </div>
    <div class="option option-3">
      Option 3 content goes here
    </div>
    <div class="option option-4">
      Option 4 content goes here
    </div>
    <div class="option option-5">
      Option 5 content goes here
    </div>
    <div class="option option-6">
      Option 6 content goes here
    </div>
  </div>

  </body>
  </html>

1 个答案:

答案 0 :(得分:4)

在收紧代码方面,这是一个开始:

 $("#payment-visual-selector :radio").change( function(){ 
    var otherClass = '.option.option-' + $(this).val();
    $('#contain-me '+otherClass)
        .addClass('chosen').show()
        .siblings().removeClass('chosen').hide();     
 });

对于标签点击未触发单选按钮change事件的问题,请尝试以下操作:

$('.label').click( function(){
  $(this).prev('input:radio')
      .attr('checked', true)
      .change(); // Manually trigger the change event
  });
});