我有这个代码,但我无法让它工作。根据你的问题是什么?
<div class="box-search">
<form action="" method="get" onsubmit="return doSearch('search-form','text_cautat');" id="search-form">
<input type="text" name="s" class="search-field" id="text_cautat" value="" maxlength="100" placeholder="Cauta" />
<select class="search-cat select-menu" name="categorie" id="cat_cautare">
<option value="0">Toate</option>
<option value="211">Accesorii GSM</option>
<option value="406">Laptopuri & Tablete</option>
<option value="193">Componente PC</option>
<option value="416">Periferice</option>
<option value="1364">Gaming PC & Console</option>
<option value="192">Foto & Video</option>
<option value="233">Electronice</option>
<option value="219">Auto</option>
<option value="1378">Telefoane</option>
</select>
<input type="button" value="Cerca" onclick="return doSearch('search-form','text_cautat');" class="search-button" />
</form>
和JS:
function doSearch(id_form, id_camp) {
var search_string = $(id_camp).value;
//trim
search_string = trim(search_string);
var search_form = $(id_form);
if (search_string.length < 3) {
alert("Minimo 3 caratteri!");
return false;
} else {
var cuvinte_split = search_string.split(" ");
search_string = search_string.replace(/-/g, "_");
search_string = search_string.replace(/[^A-Za-z0-9_.]/g, "-");
search_form.action = "http://www.website.com/cerca/" + search_string;
search_form.submit();
return true;
}
}
function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
脚本应该允许仅在至少三个字符时输入搜索。但它不起作用。
答案 0 :(得分:1)
答案 1 :(得分:0)
您需要使用$(sector)
从DOM创建jQuery对象,例如$('#search-form')
根据您的代码,您应该将onsubmit
更改为
onsubmit="return doSearch('#search-form','#text_cautat');"