狐狸.fox 2015-07-14 11:18 采纳率: 0%
浏览 20

jQuery附加把手

I'm using handlebars to render info I got from my local server using ajax. My HTML looks like:

    <ul class="nav nav-tabs" id="tabsId">
        <script id="tabs-template" type="text/x-handlebars-template">
            {{#each Tabs}}
                <li data-tab-content={{Id}}><a href="#">{{Name}}</a></li>
            {{/each}}
        </script>
    </ul>
    <div id="tabsContentId">
    <script id="tabs-content-template" type="text/x-handlebars-template">
        {{#each Tabs}}
            <div class="tab-content" data-tab-content="{{Id}}">{{Content}}</div>
        {{/each}}
    </script>
    </div>

I also have a simple form with id, name and content inputs and sumbit button. It looks like:

<input type="text" class="form-control" name="inputIndex" id="inputIndex" placeholder="Index">
<input type="text" class="form-control" name="inputTitle" id="inputTitle" placeholder="Title">
<textarea class="form-control" rows="5" name="textareaContent" id="textareaContent" placeholder="Content"></textarea>
<button type="submit" class="btn btn-success" id="buttonSumbit">Add</button>

How can I add a new tab to my html document using jQuery? I've tried to write a stub function like:

$("#buttonSumbit").click(function(){
    var id = '10';
    var name = '10';
    $("<li data-tab-content='" + id + "'><a href=\"#\">" + name + "</a></li>").appendTo("tabsId");
});

But nothing happening when I'm clicking sumbit button.

  • 写回答

2条回答 默认 最新

  • weixin_33739541 2015-07-14 11:22
    关注

    The problem is that id should be #id, as you are going to use ID selector of jQuery it should be #ID, check change required to be done below :

     $("<li data-tab-content='" + id + "'><a href=\"#\">" + content + 
                   "</a></li>").appendTo("#tabsId")
    
    评论

报告相同问题?