为了账号安全,请及时绑定邮箱和手机立即绑定

在通过 ajax 循环调用后尝试截断字符串

在通过 ajax 循环调用后尝试截断字符串

PHP
青春有我 2022-12-03 10:44:21
我有一个包含名称的表,每个条目的姓氏都需要截断。迈尔斯·戴维斯成为迈尔斯·D。我使用以下 JS 实现了这一点:$('.trunc-name').each(function (d, td) {    $(td).text($(td).text().replace(/^(\S+)\s+(\S).*/, '$1 $2.'));});td课程设置为trunc-name:<td class="trunc-name"><?php foreach ($resident_name_terms as $object) { echo $object->name; } ?></td>这些表内容(实际上是 WP 分类术语)可通过选择表单进行过滤 - 其选项也被截断了。选择:if( $terms = get_terms( array(    'taxonomy' => 'resident_name',    'orderby' => 'name'     ) ) ) :     echo '<select name="resident_name_filter"><option value="" selected="selected">' . __( 'All Residents', 'mv' ) . '</option>';     foreach ( $terms as $term ) :         echo '<option class="trunc-name" value="' . $term->term_id . '">' . $term->name . '</option>';      endforeach;     echo '</select>';endif;到目前为止一切正常。问题是,当我通过该列(使用 AJAX 构建)的选择选项更改标准时,新输出的值不会被截断。查询参数:$relation = 'AND';$params = array();$args['tax_query']['relation'] = $relation;foreach ( $taxonomies as $tax ) {    if( isset( $_POST[ $tax . '_filter' ] ) && !empty( $_POST[ $tax . '_filter' ] ) ) {        $args['tax_query'][] = array(            'taxonomy' => $tax,            'field' => 'id',            'terms' => $_POST[ $tax . '_filter' ],        );    }};环形:$query = new WP_Query( $args );if( $query->have_posts() ) : ?>    <table style="width:100%" id="incident-reports">        <tr>            <th>Resident</th>        </tr>        <?php            while( $query->have_posts() ): $query->the_post();                $resident_name_terms = get_the_terms( $post->ID, 'resident_name' );        ?>        <tr>            <td class="trunc-name"><?php foreach ($resident_name_terms as $object) { echo $object->name; } ?></td>        </tr>        <?php endwhile; ?>    </table>endif;一如既往,非常感谢任何帮助!
查看完整描述

1 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

success:创建新元素后,您需要在函数中运行截断代码。


$.ajax({

    ...

    success: function(response) {

        ... // code that adds the new HTML

        $('.trunc-name').each(function (d, td) {

            $(td).text($(td).text().replace(/^(\S+)\s+(\S).*/, '$1 $2.'));

        });

    }

});


查看完整回答
反对 回复 2022-12-03
  • 1 回答
  • 0 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号