javascript - 如何在页面上拥有多组评分

标签 javascript

我正在使用简单的星级评定器,并且必须设置评级 - http://codepen.io/anon/pen/WvOevd

  <input type="radio" name="rating" value="1"><i></i>
  <input type="radio" name="rating" value="2"><i></i>
  <input type="radio" name="rating" value="3"><i></i>
  <input type="radio" name="rating" value="4"><i></i>
  <input type="radio" name="rating" value="5"><i></i>
</span>
<strong class="choice">Choose a rating</strong>
<br>
<span class="star-rating">
  <input type="radio" name="rating" value="1"><i></i>
  <input type="radio" name="rating" value="2"><i></i>
  <input type="radio" name="rating" value="3"><i></i>
  <input type="radio" name="rating" value="4"><i></i>
  <input type="radio" name="rating" value="5"><i></i>
</span>
<strong class="choice">Choose a rating</strong>

如您所见,通过单击一组,它将更改/影响另一组评级。我应该怎样做才能拥有两个功能集?

最佳答案

Here is your exemple working

这是因为他们都使用相同的组(评级)

尝试将第二组更改为其他类似的内容:

<span class="star-rating">
  <input type="radio" name="otherGroupName" value="1"><i></i>
  <input type="radio" name="otherGroupName" value="2"><i></i>
  <input type="radio" name="otherGroupName" value="3"><i></i>
  <input type="radio" name="otherGroupName" value="4"><i></i>
  <input type="radio" name="otherGroupName" value="5"><i></i>
</span>
//I also changed your class here to have a unique one
<strong class="choiceOtherGroupName">Choose a rating</strong>

我还将你的js更改为:

$(':radio').change(
  function(){
    if (this.name === "rating")
      $('.choiceRating').text( this.value + ' stars' );
    else if(this.name === "otherGroupName")
      $('.choiceOtherGroupName').text( this.value + ' stars' );
  } 
)

此代码将检查您的单选按钮组以定位正确的标签!如果是来自组评级,则会输出到 choiceRating,如果是来自第二组(choiceOtherGroupName),则会输出到第二个标签!

关于javascript - 如何在页面上拥有多组评分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30742703/

相关文章:

javascript - 根据值删除本地存储数组对象

javascript - (A-Frame) 本地 gltf 不会加载;无法读取未定义的属性 'slice'

javascript - div 中的按钮都带有 onclick 事件 - 如何在单击按钮时仅触发两者之一?

javascript - GridX为行设置类(基于行数据)

javascript - 在使用 ajax 通过 html 表单发送之前验证电子邮件地址

javascript - stats.nba.com 的 CORS/CORB

javascript - 如何从 JavaScript 文件中正确解压编码为 JSON 的 PHP 数组中的数据

javascript - 在两个 Angular 分量之间共享数据

javascript - 输入提交按钮在 Android 设备上不起作用

javascript - 如何在 Javascript 中用视频标签替换音频标签或用另一个标签替换一个标签