【问题标题】:Display the text label for selected radio button in Jquery在 Jquery 中显示选定单选按钮的文本标签
【发布时间】:2010-03-23 11:56:02
【问题描述】:

喂,

我有一组单选按钮,我可以使用 jQuery 选择选定的值,但不能选择选定值的文本标签。

例如:

<input type="radio" value="1" name="priority">High</input>
<input type="radio" value="2" name="priority">Medium</input>
<input type="radio" value="3" name="priority">Low</input>

查询代码以选择所选值

jQuery('input:radio[name=priority]').change(function()
{
 var priority_type=jQuery(this).attr("value");
        alert(priority_type);
}

OUTPUT 可以是以下 (1,2,3) 中的任何一个

现在我的要求是,我想显示所选值的标签 例如(高或低或中)取决于单选按钮的选择。

希望这会有所帮助。如果您有任何问题,请告诉我。请帮助我完成这项任务

【问题讨论】:

    标签: jquery


    【解决方案1】:

    我的第一个想法是 text() 会正常工作。不幸的是,收音机没有 innerText。您可以将标签与单选一起使用,并将 for 属性指定为单选按钮 ID。类似的东西

    .text()

    jQuery('input:radio[name=priority]').change(function()
    {
       var id = $(this).attr("id");
       alert($('label[for='+id+']').text());
    }
    
    <input type="radio" name="priority" value="1" id="rdLow" />
    <label for="rdLow">Low</label> 
    <input type="radio" name="priority" value="2" id="rdMedium" />
    <label for="rdMedium">Medium</label> 
    <input type="radio" name="priority" value="3" id="rdHigh" />
    <label for="rdHigh">High</label> 
    

    查看working demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      相关资源
      最近更新 更多