3 回答

TA貢獻1827條經(jīng)驗 獲得超9個贊
我想你正在尋找這個。jquery/自動完成
$(".search-autocomplete").autoComplete({
minChars: 1,
source: function (term, suggest) {
term = term.toLowerCase();
var choices = [
{ label: "a", value: "apple" },
{ label: "b", value: "boy" },
{ label: "c", value: "car" },
];
var suggestions = [];
for (i = 0; i < choices.length; i++)
if (choices[i].value.indexOf(term) !== -1) {
suggestions.push(choices[i]);
}
suggest(suggestions);
},
renderItem: function (item, search) {
return (
'<div class="autocomplete-suggestion">' +
'<span style="color: red; margin-right: 10px">' +
item.label +
"</span>" +
"<span>" +
item.value +
"</span>" +
"</div>"
);
},
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.css" rel="stylesheet"/>
<input
type="text"
autocomplete="off"
value=""
name="s"
class="search-autocomplete"
id="s"
placeholder="My Search form"
/>

TA貢獻1829條經(jīng)驗 獲得超9個贊
您需要創(chuàng)建唯一的標簽集,或者可以循環(huán)遍歷結(jié)果集并僅附加標簽一次
var headers = [];
if(!headers.includes(section[i])){? ?//section will contains the lables? ? ? ? ?
? ? label = document.createElement("H2");?
? ? label.innerHTML = section[i];
? ? headers.push(section[i]);? ? ? ? ? ??
? ? resultSet.appendChild(label);
}

TA貢獻2041條經(jīng)驗 獲得超4個贊
function ja_ajax_search() {
$results = new WP_Query( array(
'post_type' => array( 'product','product-tag' ,'post_author'),
'post_status' => 'publish',
'nopaging' => true,
'posts_per_page'=> 100,
's' => stripslashes( $_POST['search'] ),
) );
$items = array();
// print_r($results);
if ( !empty( $results->posts ) ) {
foreach ( $results->posts as $result) {
// $services = [$result ];
$post_details = [
'post_id' => $result->ID,
'post_author' => $result->post_author,
'post_title' => $result->post_title,
'post_status' => $result->post_status,
'post_slug' => $result->post_name
];
array_push($items, $post_details);
// $items[] .= $services;
// echo $result->post_title;
}
}
// print_r($items);
// print_r($items);
// print_r($items);
// if ( !empty( $results->posts ) ) {
// foreach ( $results->posts as $result ) {
// $items[] = $result->post_status;
// }
// }
wp_send_json_success( $items );
}
add_action( 'wp_ajax_search_site', 'ja_ajax_search' );
add_action( 'wp_ajax_nopriv_search_site', 'ja_ajax_search' );
- 3 回答
- 0 關(guān)注
- 192 瀏覽
添加回答
舉報