【问题标题】:Fieldset onclick getting radio button clicked valueFieldset onclick 获取单选按钮点击值
【发布时间】:2016-12-21 20:37:04
【问题描述】:

我的 HTML 中有一个字段集,它基本上代表 5 星评级系统,如下所示:

<fieldset class="rating" id="ratingSystem">
    <input type="radio" id="star5" name="rating" value="5" /><label class="full" for="star5" title="Awesome - 5 stars"></label>
    <input type="radio" id="star4" name="rating" value="4" /><label class="full" for="star4" title="Pretty good - 4 stars"></label>
    <input type="radio" id="star3" name="rating" value="3" /><label class="full" for="star3" title="Meh - 3 stars"></label>
    <input type="radio" id="star2" name="rating" value="2" /><label class="full" for="star2" title="Kinda bad - 2 stars"></label>
    <input type="radio" id="star1" name="rating" value="1" /><label class="full" for="star1" title="Sucks big time - 1 star"></label>
</fieldset>

我已经定义了一个 onclick 事件,它定义了用户何时单击某些星级评分(单选按钮)然后我只是出于测试目的显示单击的值,但我总是显示双精度值而不是显示一个……

例如,如果我点击 5,我会显示在控制台中:

undefined  
5

我第二次点击 4 我得到了值:

5
4

我使用的代码是:

$('#ratingSystem').click(function () {
        console.log($('input[name=rating]:checked').val());
    });

我在这里做错了什么?

编辑,这些是我用来将单选按钮变成星星的 CSS 类,使其看起来像星级评分系统:

fieldset, label { margin: 0; padding: 0; }
body{ margin: 20px; }
h1 { font-size: 1.5em; margin: 10px; }

/****** Style Star Rating Widget *****/

.rating { 
  border: none;
  float: left;
}

.rating > input { display: none; } 
.rating > label:before { 
  margin: 5px;
  font-size: 1.25em;
  font-family: FontAwesome;
  display: inline-block;
  content: "\f005";
}

.rating > .half:before { 
  content: "\f089";
  position: absolute;
}

.rating > label { 
  color: #ddd; 
 float: right; 
}

/***** CSS Magic to Highlight Stars on Hover *****/

.rating > input:checked ~ label, /* show gold star when clicked */
.rating:not(:checked) > label:hover, /* hover current star */
.rating:not(:checked) > label:hover ~ label { color: #FFD700;  } /* hover previous stars in list */

.rating > input:checked + label:hover, /* hover current star when changing rating */
.rating > input:checked ~ label:hover,
.rating > label:hover ~ input:checked ~ label, /* lighten current selection */
.rating > input:checked ~ label:hover ~ label { color: #FFED85;  } 

这些类中的任何一个都可能是问题的根源吗?

【问题讨论】:

  • 当我运行 jsfiddle 中提供的代码时,您能提供更多信息吗?我没有遇到任何问题。 jsfiddle.net/fq56bxzz
  • 你确定没有另一组input[name=rating] 在页面上的其他地方,可能是隐藏的吗?
  • @User987 完美,这可能是 css 是一些阻塞元素。我自己也遇到过几次这个问题。
  • @User987 我添加了css,现在我遇到了问题。一会儿我会帮你解答的:)
  • 我认为伪元素在乱搞

标签: javascript jquery asp.net onclick fieldset


【解决方案1】:

尝试将您的事件附加到每个 input 元素:

$('#ratingSystem input').click(function() {
  console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<fieldset class="rating" id="ratingSystem">
  <input type="radio" id="star5" name="rating" value="5" />
  <label class="full" for="star5" title="Awesome - 5 stars"></label>
  <input type="radio" id="star4" name="rating" value="4" />
  <label class="full" for="star4" title="Pretty good - 4 stars"></label>
  <input type="radio" id="star3" name="rating" value="3" />
  <label class="full" for="star3" title="Meh - 3 stars"></label>
  <input type="radio" id="star2" name="rating" value="2" />
  <label class="full" for="star2" title="Kinda bad - 2 stars"></label>
  <input type="radio" id="star1" name="rating" value="1" />
  <label class="full" for="star1" title="Sucks big time - 1 star"></label>
</fieldset>

【讨论】:

    【解决方案2】:

    您可以将另一个事件附加到 checkbox 的其他地方,但您可以根据您的 OP 代码检查下面的工作 sn-p 显示 console.log 仅在单击一次时触发。

    我建议直接将点击附加到输入,仅使用以下方式获取当前检查值:

    $(this).val();
    

    希望这会有所帮助。

    $('#ratingSystem input').click(function () {
      console.log($(this).val());
    });
    fieldset, label { margin: 0; padding: 0; }
    body{ margin: 20px; }
    h1 { font-size: 1.5em; margin: 10px; }
    
    /****** Style Star Rating Widget *****/
    
    .rating { 
      border: none;
      float: left;
    }
    
    .rating > input { display: none; } 
    .rating > label:before { 
      margin: 5px;
      font-size: 1.25em;
      font-family: FontAwesome;
      display: inline-block;
      content: "\f005";
    }
    
    .rating > .half:before { 
      content: "\f089";
      position: absolute;
    }
    
    .rating > label { 
      color: #ddd; 
      float: right; 
    }
    
    /***** CSS Magic to Highlight Stars on Hover *****/
    
    .rating > input:checked ~ label, /* show gold star when clicked */
    .rating:not(:checked) > label:hover, /* hover current star */
    .rating:not(:checked) > label:hover ~ label { color: #FFD700;  } /* hover previous stars in list */
    
    .rating > input:checked + label:hover, /* hover current star when changing rating */
    .rating > input:checked ~ label:hover,
    .rating > label:hover ~ input:checked ~ label, /* lighten current selection */
    .rating > input:checked ~ label:hover ~ label { color: #FFED85;  } 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <fieldset class="rating" id="ratingSystem">
      <input type="radio" id="star5" name="rating" value="5" /><label class="full" for="star5" title="Awesome - 5 stars"></label>
      <input type="radio" id="star4" name="rating" value="4" /><label class="full" for="star4" title="Pretty good - 4 stars"></label>
      <input type="radio" id="star3" name="rating" value="3" /><label class="full" for="star3" title="Meh - 3 stars"></label>
      <input type="radio" id="star2" name="rating" value="2" /><label class="full" for="star2" title="Kinda bad - 2 stars"></label>
      <input type="radio" id="star1" name="rating" value="1" /><label class="full" for="star1" title="Sucks big time - 1 star"></label>
    </fieldset>

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 2017-02-06
      相关资源
      最近更新 更多