我有一個包含多個複選框和標籤的表單,其中大部分功能都正常工作,但我無法獲取複選框的標籤值,同時剝離了標籤:使用jQuery從字符串中刪除HTML標記
<input type="checkbox" id="list-item-x" /> <label for="list-item-x"><span class="date">Oct 1</span> The checkbox label</label>
我想label
元素,但沒有span.date
。我該如何刪除它?我已經試過如下:
$('label').remove('span.date').text();
那沒有工作,也沒有執行以下操作:
$('label').find('span.date').text();
如何我只能去除span
元素,獨自一人出走label
文本供以後使用?
編輯:這是整個(亂)代碼:
$('input.list-item-checkbox').live('click', function(){
var myself = $(this);
var my_id = $(myself).attr('id').split('-', 3);
my_id = parseInt(my_id[2]);
var my_label = $(myself).parent().children('label').html();
var parent = $(myself).parents('.list-item');
if ($(this).is(':checked'))
{
$.ajax({
type: 'POST',
url: CI.base_url + 'ajax/unfinished_item/',
data: 'list-item=' + my_id,
dataType: 'json',
success: function(result){
if (result.status == 'ERROR')
{
alert('Could not edit item');
}
else
{
var html_string =
'<div class="list-item">' +
' <input type="checkbox" class="list-item-checkbox" id="list-item-' + my_id + '" /> ' +
' <label for="list-item-' + my_id + '">' + my_label + '</label>' +
'</div>';
$(myself).parents('.list-finished').siblings('.list-items').append(html_string);
$(myself).parents('.list-item-finished').remove();
// This is where I need to get the span element removed from the label
}
},
error: function(){
alert('No contact with server');
}
});
}
});
是否要刪除跨度元素或跨度內的文本? – rahul 2009-10-01 13:53:57
整個跨度元素。 – 2009-10-01 13:56:24
你能發佈完整的代碼嗎? – rahul 2009-10-01 14:06:19