Javascript 我无法从下划线模板中获取字段

Javascript 我无法从下划线模板中获取字段,javascript,jquery,Javascript,Jquery,我有一个代码,它使用下划线框架在页面中动态附加表单字段组,我在获取模板的输入字段时遇到问题,因此我不知道我是否做了一些看不到的错误 下划线模板如下所示: <script type="text/template" id="redirection_url_element"> <tr> <td> <input type="text" class="regular-text" placeholder="<?p

我有一个代码,它使用
下划线
框架在页面中动态附加表单字段组,我在获取模板的输入字段时遇到问题,因此我不知道我是否做了一些看不到的错误

下划线
模板如下所示:

<script type="text/template" id="redirection_url_element">
    <tr>
        <td>
            <input type="text" class="regular-text" placeholder="<?php esc_attr_e( 'Enter the redirection URL', 'WPL' ); ?>" value="<%= (typeof( redirection_url ) !== 'undefined') ? redirection_url : '' %>" data-name="redirection_url" data-group="redirection" />
        </td>
        <td>
            <input type="text" class="regular-text" placeholder="<?php esc_attr_e( 'Weight', 'WPL' ); ?>" value="<%= (typeof( redirection_weight ) !== 'undefined') ? redirection_weight : '' %>" data-name="redirection_weight" data-group="redirection" />
        </td>
        <td>
            <i class="remove-parent-row">&times;</i>
        </td>
    </tr>
</script>
console.log ( $ ( '#redirection_url_element' ).find( ':input' ) );
var $template_result = _.template ( $ ( '#redirection_url_element' ).html (), $data )
console.log ( $( $.parseHTML( $ ( '#redirection_url_element' ).text() ) ).find( ':input' ) );
我得到以下结果:

prevObject: e.fn.init[1], context: document, selector: "#redirection_url_element :input"]
    context: document
    length: 0
    prevObject: e.fn.init[1]
    selector: "#redirection_url_element :input"
    __proto__: m[0]
而且它似乎找不到输入字段。如果您看到长度为
0

你认为我做错什么了吗?有什么我看不见的错误吗

更新#1

我使用模板的方式如下所示:

<script type="text/template" id="redirection_url_element">
    <tr>
        <td>
            <input type="text" class="regular-text" placeholder="<?php esc_attr_e( 'Enter the redirection URL', 'WPL' ); ?>" value="<%= (typeof( redirection_url ) !== 'undefined') ? redirection_url : '' %>" data-name="redirection_url" data-group="redirection" />
        </td>
        <td>
            <input type="text" class="regular-text" placeholder="<?php esc_attr_e( 'Weight', 'WPL' ); ?>" value="<%= (typeof( redirection_weight ) !== 'undefined') ? redirection_weight : '' %>" data-name="redirection_weight" data-group="redirection" />
        </td>
        <td>
            <i class="remove-parent-row">&times;</i>
        </td>
    </tr>
</script>
console.log ( $ ( '#redirection_url_element' ).find( ':input' ) );
var $template_result = _.template ( $ ( '#redirection_url_element' ).html (), $data )
console.log ( $( $.parseHTML( $ ( '#redirection_url_element' ).text() ) ).find( ':input' ) );
这就是我动态地将模板转换为HTML的方式,我不使用
主干网。我只是将
$template\u结果
注入目标元素

更新#2

所以,@Wolff,这张桌子看起来是这样的:

<table class="repeater_table">
    <tbody id="redirection_url_container">
        <tr>
            <td>
                <input type="text" class="regular-text" placeholder="Enter the redirection URL" value="" data-name="redirection_url" data-group="redirection">
            </td>
            <td>
                <input type="text" class="regular-text" placeholder="Weight" value="" data-name="redirection_weight" data-group="redirection">
            </td>
            <td>
                <i class="remove-parent-row">×</i>
            </td>
        </tr>
    </tbody>
</table>
我知道它有点难看,但它确实适合我。你认为有没有更好的方法来实现上述目标?

试试看

$( '#redirection_url_element' ).find( 'input' )
试一试


不能选择
标记中的内容,因为它们不是DOM结构的一部分

是否尝试对目标元素运行选择器

样本:

<div id="test"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(function() {
  $('#test').html('<span id="foo">Foo bar</span>');
  console.log($('#foo'));
});
</script>

$(函数(){
$('#test').html('foobar');
console.log($('foo'));
});

请记住,如果浏览器不在
元素中,则会删除
tr
元素

您不能选择
标记中的内容,因为它们不是DOM结构的一部分

是否尝试对目标元素运行选择器

样本:

<div id="test"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(function() {
  $('#test').html('<span id="foo">Foo bar</span>');
  console.log($('#foo'));
});
</script>

$(函数(){
$('#test').html('foobar');
console.log($('foo'));
});


请记住,如果浏览器不在
元素中,则会删除
tr
元素

请注意,在模板中,您应该避免应用程序的逻辑。例如:
value=”“
。您可以在主干视图中写入三元条件,并将正确的值传递给模板,即必须写入
value=”“
,并且可以为空,没有问题。
元素不包含HTML。它包含文本。jQuery的选择器在这里不起作用。所以@Tomalak有其他解决方案吗?@MarcosPérezGude不使用
主干网。我使用
jQuery
并动态翻译
下划线
。@MerianosNikos您必须以jQuery呈现的HTML为目标,所以它看起来如何?只是作为一个说明,在模板中您应该避免应用程序的逻辑。例如:
value=”“
。您可以在主干视图中写入三元条件,并将正确的值传递给模板,即必须写入
value=”“
,并且可以为空,没有问题。
元素不包含HTML。它包含文本。jQuery的选择器在这里不起作用。所以@Tomalak有其他解决方案吗?@MarcosPérezGude不使用
主干网。我使用
jQuery
并动态地翻译
下划线
。@MerianosNikos您必须在jQuery呈现的HTML中找到目标,它看起来是什么样子?为什么要对此进行否决?我认为这比
find(':input')要好。
关于性能可能更好,但不能肯定地解释OP的问题。也许OP并没有将
作为表单控件,并且仍然希望选择所有的注销类型。所以在这种情况下,最好不是我否决了你,但我已经尝试过了,没有意义。好的,谢谢你的解释@A.Wolff。显然,这是一个低质量的回答,但我不理解反对票。我做了,但仍然没有工作。我找到了解决方案,在第三次更新我的问题时就可以找到了。谢谢你的efort,为什么不投票?我认为这比
find(':input')要好。
关于性能可能更好,但不能肯定地解释OP的问题。也许OP并没有将
作为表单控件,并且仍然希望选择所有的注销类型。所以在这种情况下,最好不是我否决了你,但我已经尝试过了,没有意义。好的,谢谢你的解释@A.Wolff。显然,这是一个低质量的回答,但我不理解反对票。我做了,但仍然没有工作。我找到了解决方案,在第三次更新我的问题时就可以找到了。谢谢你的帮助,问题已经解决了。如果您看到我的问题的第三次更新,您可以看到解决方案。此外,我认为你的答案可能超出了范围,所以我不会投反对票:)但请记住,你可能会被其他成员投反对票。非常感谢你的帮助,问题已经解决了。如果您看到我的问题的第三次更新,您可以看到解决方案。此外,我认为你的答案可能超出了范围,所以我不会投反对票:)但请记住,你可能会被其他成员投反对票。非常感谢你的帮助。