jQuery,在HTML模板中查找控件

Chu*_*way 1 jquery

我正在使用html模板:

<script id="locationTemplate" type="application/template" >

    <p>
        <input id="searchText" type="text" />
        <input id="searchlocation" type="submit" value="Search" />
    </p>

    <p>
        <label>Location Name</label>
        <input id="locationName" type="text" />
    </p>


    <div id="map"></div>

</script>
Run Code Online (Sandbox Code Playgroud)

我可以加载模板,但是当我尝试在模板中找到控件时,我不能.

    this.template = $('#locationTemplate');
    this.searchText = $(this.template.html()).find('input#searchText');
    this.locationName = this.template.find('p input#locationName');
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?我尝试了两种不同的方法.

更新:

我有这个代码工作:

    this.template = $('#locationTemplate');
    this.searchText = $(this.template.html()).find('input#searchText');
    this.locationName = $(this.template.html()).find('input#locationName');
Run Code Online (Sandbox Code Playgroud)

但我很困惑为什么我必须将html转储到另一个jQuery实例.为什么我不能只使用template.find方法,因为模板已经包装在jQuery中...

gof*_*rie 5

您不能将模板放在<script>标记中.它会阻止解析器解析内部的东西,因此它不会显示在DOM中,因此选择器将无法处理它.