首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在选择时更改单选按钮项类

在选择时更改单选按钮项类
EN

Stack Overflow用户
提问于 2015-04-28 04:32:16
回答 3查看 563关注 0票数 0

我正在使用Razor视图引擎生成一个使用此代码的RadioButton项目列表,我的css和Javascript是

代码语言:javascript
运行
复制
    .radioSelection
{
    color: Green;
    font-weight: bold;        
}
 $(document).ready(function () {         
     $("input[name=ScoreId]:radio").change(function () {   
        //How can i access the label span of current selection & reset the old selection         
     });
 });

@foreach (var item in model)
{
  <li class="radio clearfix ui-sortable-handle">     
     @Html.RadioButton("ScoreId", statement_item.Id, new { @class = "" })
    <span class="lbl" >@statement_item.Statement</span>
  </li>
}

如何将类radioSelection设置为选定的radiobuttonList项,以及如何在选择新的无线电项时将旧项的选择重置为正常

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-04-28 04:34:45

使用.next()函数获取所需的下一个元素span

代码语言:javascript
运行
复制
$("input[name=ScoreId]:radio").change(function () { 

    $(':radio:checked').not(this).prop('checked',false)
    $('li.radioSelection').removeClass('radioSelection'); //Reset selection

    $(this).closest('li').addClass('radioSelection');   //Add class to list item

    $(this).next('span')         //Access span
});
票数 1
EN

Stack Overflow用户

发布于 2015-04-28 04:34:06

试试- $(this).siblings('span.lbl')

票数 0
EN

Stack Overflow用户

发布于 2015-04-28 04:45:20

试试这个:

代码语言:javascript
运行
复制
 $(document).ready(function () {         
  $("input[name=ScoreId]:radio").change(function () {   

  $(this).prop('checked', false); }) // make check only selected radio button and uncheck others

  $('input:radio[name=ScoreId]').each(function () {         
     $(this).removeClass('className'); })   // remove the class from all radio buttons         
  }    

  $("input:radio[name=ScoreId]:checked").closest('li').addClass('radioSelection');  // add class only to selected radio button  

  $(input:radio[name=ScoreId]:checked).next('span');         // Access span of selected radio
    });
 });     
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29910387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档