亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在通過 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 關注
  • 95 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號