Javascript 从PHP调用的jQuery代码赢得';不执行

Javascript 从PHP调用的jQuery代码赢得';不执行,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我正在开发一个Wordpress站点,它(当然)由多个PHP模板组成。我遇到问题的特定模板片段调用如下: <script id="templatehidden"> <?php get_template_part( 'fragments/calculator', 'row-sa' ); ?> </script> 我的问题是php中的img元素。当页面第一次加载时,它执行其工作并删除

我正在开发一个Wordpress站点,它(当然)由多个PHP模板组成。我遇到问题的特定模板片段调用如下:

<script id="templatehidden">                                    
    <?php get_template_part( 'fragments/calculator', 'row-sa' ); ?>
</script>

我的问题是php中的img元素。当页面第一次加载时,它执行其工作并删除其所在的行。然而,在我添加更多行之后,似乎不再有javascript代码在.form内容中执行。有什么问题吗?

假设您使用javascript添加行,您需要事件委派:在绑定偶数处理程序时,元素不存在,添加它们时,
单击事件不会自动绑定到这些新元素,除非您使用事件委派

您可以使用以下方法轻松更改此设置,例如:

$('.form-content').on('click', '.remove-field-sa', function () {           
    $(this).closest('.form-row').remove();
});
注意这里的
.formcontent
元素必须在页面加载时出现。如果不是,您也可以使用以下内容:

// Any parent element that is present at page-load and will contain the child elements will do.
$('body').on('click', '.remove-field-sa', function () {           
    $(this).closest('.form-row').remove();
});

美好的委托函数不也是可能的吗?也许只是为了以防万一jquery稍后会更新。@baboizk是的,但是请注意,
on()
并没有被弃用。您是否正在考虑
live()
?是的,就是这样!live()已被on()替换。抱歉,谢谢!成功了!看起来我有很多关于事件、处理者和委托的知识需要学习,但至少现在,它起了作用。
$('.form-content').on('click', '.remove-field-sa', function () {           
    $(this).closest('.form-row').remove();
});
// Any parent element that is present at page-load and will contain the child elements will do.
$('body').on('click', '.remove-field-sa', function () {           
    $(this).closest('.form-row').remove();
});