在我的
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/