0

基本上我的搜索以跨度返回搜索结果,然后当单击一个时,一个新的跨度将添加到另一个具有选择输入和隐藏输入的 div,因此所有选定的功能都可以作为数组发布。

我的问题是,$(this).html()现在包括一个跨度的 class alias_span。我不想出现在新的跨度中。如何在将单击的跨度的内容插入新跨度之前将其删除

$(".minisearch_res").live('click', function() { 

    // when a search result is clicked we disable the submit button, 
    // append a span to the left column with an id, 
    // a select input to select standard/optional and
    // a hidden field with the required information to save and something to 
    // show the user


    var id = $(this).attr('id');

    var html = "<span class= \"added_result_cont\" id=\"" + id + "_cont\">";
    html += "<select name='" + id + "_sel'>";
    html += "<option value=\"none\" selected=\"selected\"></option>";
    html += "<option value=\"std\">Standard</option>";
    html += "<option value=\"opt\" >Optional</option>";
    html += "</select>";
    html += "<span id= \"" + id + "\" class=\"added_result\">";
    html += "<input type=\"hidden\" name=\"selectedfeat[]\" value=\"" + id + "\">";
    html += $(this).html() + "</span></span>";

    $('#div_apply_to').append(html);

    $(this).remove();
    $('#search_input').trigger('keyup');
    $("input[type=submit]").attr("disabled", "disabled");
});

更新:这是跨度的 html

<span class="minisearch_res" id="opt_1">Anti-Lock Brakes<br><span style="padding-left:20px" class="alias_span"><i>abs</i></span><br></span>
4

2 回答 2

0
<div></div>

将其添加到 dom,然后将其删除。

var htm = (
    '<span class="foo">foo</span>' +
    '<span>bar</span>' +
    '<span>bar</span>' +
    '<span>bar</span>' +
    '<span>bar</span>'
);

var $el = $("div").append(htm);
console.log($el.html());
$el.find('.foo').remove();
console.log($el.html());

http://jsfiddle.net/chovy/w2HdE/

于 2012-12-24T09:16:31.837 回答
0

有一个整洁的跨浏览器解决方案,我对此感到非常自豪:)

var temp = $(this);
temp.children('.alias_span').remove();
 $('#div_apply_to').append("...html..." + temp.html() + "...html...");

灵感来自这里: Remove element from Ajax Response

于 2012-12-25T02:24:57.503 回答