jQuery给出输入ID,获取标签文本

在我的 HTML表单(如大多数 HTML表单)中,标签与字段共享相同的ID.一旦点击了具有匹配ID的复选框,我试图返回标签标签的HTML.
<input id="yes1" type="checkBox">
<label for="yes1">This is the text it should return.</label>

和我的jQuery:

$('input').change(function() {
    if (this.checked) {
        var response = $('label#' + $(this).attr('id')).html();
    }
}

但唉,响应出来是空的.

解决方法

$('input').change(function() {
    if (this.checked) {
        var response = $('label[for="' + this.id + '"]').html();
    }
});

不需要从属性获取id.

演示:http://jsfiddle.net/wycvy/

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...