为什么我的HTML替换分配不起作用?

时间:2015-03-26 20:06:06

标签: jquery html visual-studio-2013 asp.net-webpages

选择后,我想用其他内容替换当前内容。我正在尝试替换(现在),当从下拉列表中选择“Tom Sawyer”时,从先前的选择更改为“稍后将添加Tom Sawyer”。但它不起作用 - 现有的HTML(HuckFinn)仍然存在于div中。以下是相关代码:

$('#fictionDropDown').change(function () {
    configLoading();
    var sel = this.value;
    if ((sel == "HuckFinn") && (currentFictionSelection != "HuckFinn")) {
        $('#FictionContent').load('Content/HuckFinn.html');
        currentFictionSelection = "HuckFinn";
    }
    else if ((sel == "TomSawyer") && (currentFictionSelection != "TomSawyer")) {
        $("#FictionContent").empty();
        $('#FictionContent').html('Tom Sawyer will be added later');
        currentFictionSelection = "TomSawyer";
    }

...和HTML:

<div id="tabs" class="content-wrapper">
    <ul>
        <li><a href="#tab-Fiction">Fiction</a></li>
        <li><a href="#tab-Nonfiction">Nonfiction</a></li>
        <li><a href="#tab-MiscTwain">Miscellaneous</a></li>
        <li><a href="#tab-Media">Media</a></li>
    </ul>
    <div id="tab-Fiction">
        <select id="fictionDropDown">
            <option value="HuckFinn">Huck Finn</option>
            <option value="TomSawyer">Tom Sawyer</option>
            <option value="tPatP">Prince and the Pauper</option>
            <option value="ConnYank">Connecticut Yankee</option>
            <option value="Gilded">The Gilded Age</option>
            <option value="Puddnhead">Pudd'nhead Wilson</option>
            <option value="ShortStories">Short Stories</option>
        </select>
        <div id="FictionContent" class="clearfix">Content in Fiction tab</div>
    </div>

如果有办法调试此代码,将会有所帮助。我在代码中有断点,但没有一个被击中。

无论如何,为什么清空html然后将其设置为“Tom Sawyer将在以后添加”不起作用?

1 个答案:

答案 0 :(得分:2)

使用值初始化currentFictionSelection,现在一切正常:

var currentFictionSelection = 'somevalue';

$('#fictionDropDown').change(function () {
    //configLoading();
    var sel = this.value;
    if ((sel == "HuckFinn") && (currentFictionSelection != "HuckFinn")) {
        alert('foo');
        $('#FictionContent').load('Content/HuckFinn.html');
        currentFictionSelection = "HuckFinn";
    } else if ((sel == "TomSawyer") && (currentFictionSelection != "TomSawyer")) {
        alert('bar');
        $("#FictionContent").empty();
        $('#FictionContent').html('Tom Sawyer will be added later');
        currentFictionSelection = "TomSawyer";
    }
});

演示:http://jsfiddle.net/BenjaminRay/u8rz59Lm/