【问题标题】:Accessing label text via jquery通过 jquery 访问标签文本
【发布时间】:2015-04-07 03:12:06
【问题描述】:

有没有办法使用 jquery 从<label> 元素中获取文本?因为我们可以使用 $(this).val(); 从中获取值,但无法对标签文本执行相同操作。

似乎我们应该能够在选择单选按钮时使用$(this) 查询<label> 文本,因为$(this) 在此期间知道它的值。

最初使用 case 语句并手动填写一些文本,但如果我们能够只插入标签文本,那么就不需要使用所有 case 语句了。

想法/建议?

html

<div class="panel panel-default">
                    <div class="panel-heading">
                        <h4 class="panel-title">
                            <a data-toggle="collapse" data-parent="#accordion" href="#accordion-second">What You Are Interested In Learning About</a>
                        </h4>
                    </div>
                    <div id="accordion-second" class="panel-collapse collapse">
                        <div class="panel-body">
                            <div class="radio">
                                <label><input type="radio" name="optradio2" value="2a">the Automotive Industry</label>
                            </div>
                            <div class="radio">
                                <label><input type="radio" name="optradio2" value="2b">the sports industry</label>
                            </div>
                            <div class="radio">
                                <label><input type="radio" name="optradio2" value="2c">the music industry</label>
                            </div>
                        </div>
                    </div>
                </div>

jquery

<script>
    $('[name="optradio2"]').on('change', function () {
        $('#accordion-third').slideToggle();
        $('#accordion-second').slideToggle();

        //how do you get the label text 
        var labelText = $("label", this).text();

        //current method but would prefer to just insert the label text
        /*var value = $(this).val();
        switch (value) {
            case "2a":
                $('#blank2', $('#myModal')).text('the automotive industry');
                break;
            case "2b":
                $('#blank2', $('#myModal')).text('the sports industry');
                break;
            case "2c":
                $('#blank2', $('#myModal')).text('the music industry');
                break;
            default:
                alert("oops! something went wrong");
        }*/
})
</script>

【问题讨论】:

  • $('input:radio[name=optradio2]:checked').closest('label').text();
  • 这个解决方案也很有效。谢谢斯蒂芬!

标签: jquery html asp.net-mvc forms radio-button


【解决方案1】:

上下文选择器使用find 并搜索后代。

标签不是输入的孩子,是相反的,所以你必须寻找父母,closest似乎适合

var labelText = $(this).closest("label").text();

【讨论】:

    【解决方案2】:

    您可以使用HTMLInputElement.labels 属性访问相关的&lt;label&gt;,该属性返回与特定&lt;input&gt; 关联的&lt;label&gt; 元素的NodeList,使用数组索引表示法获取(通常)第一个:

    this.labels[0].textContent;
    

    参考资料:

    【讨论】:

    • 我不断收到错误Unable to get property of '0' of undefined or null reference
    • 您的浏览器可能没有实现该属性。虽然我会仔细检查您的 this 实际上是 &lt;input&gt;
    • 我使用的是 IE,所以它可能是浏览器。在调试期间,它确实显示this[object HTMLInputElement];也可以看到childNodes = [object NodesList] -- 但是长度是0
    猜你喜欢
    • 2013-12-01
    • 2017-11-28
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2022-07-31
    • 2012-04-18
    相关资源
    最近更新 更多