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

我有一个包含名称的表,每个条目的姓氏都需要截断。迈尔斯·戴维斯成为迈尔斯·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;

一如既往,非常感谢任何帮助!


青春有我
浏览 95回答 1
1回答

慕桂英3389331

success:创建新元素后,您需要在函数中运行截断代码。$.ajax({&nbsp; &nbsp; ...&nbsp; &nbsp; success: function(response) {&nbsp; &nbsp; &nbsp; &nbsp; ... // code that adds the new HTML&nbsp; &nbsp; &nbsp; &nbsp; $('.trunc-name').each(function (d, td) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(td).text($(td).text().replace(/^(\S+)\s+(\S).*/, '$1 $2.'));&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP