我有一個包含名稱的表,每個條目的姓氏都需要截斷。邁爾斯·戴維斯成為邁爾斯·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.'));
});
}
});
- 1 回答
- 0 關注
- 95 瀏覽
添加回答
舉報
0/150
提交
取消