简体   繁体   English

JavaScript中的验证搜索字段

[英]Validation search field in javascript

I have this code, but I just can not make it work. 我有此代码,但我无法使其正常工作。 What according to you is the problem? 根据您的问题是什么?

<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 &amp; Tablete</option>
        <option value="193">Componente PC</option>
        <option value="416">Periferice</option>
        <option value="1364">Gaming PC &amp; Console</option>
        <option value="192">Foto &amp; 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>

and JS: 和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"), "");
}

The script should allow the searching have been entered only if at least three characters. 该脚本应仅在至少三个字符的情况下才允许输入搜索内容。 But it does not work. 但这行不通。

http://jsfiddle.net/ZP8R9/ http://jsfiddle.net/ZP8R9/

  1. When searching for an ID, the selector needs to start with # . 搜索ID时, 选择器必须以#开头。
  2. You also should be using val . 您还应该使用val
  3. And why not consider using jQuery's trim . 并且为什么不考虑使用jQuery的trim
  4. Also look at Unobtrusive Javascript and consider using jQuery's on . 还请查看不引人注目的Javascript,并考虑on使用jQuery。
  5. If you are going to post a jsFiddle that uses jQuery, make sure to also include the library. 如果要发布使用jQuery的jsFiddle ,请确保还包括该库。
  6. Checking what was logged (and posting it here) in the console would have led you to the problem. 控制台中检查记录的内容(并将其发布到此处)会导致您遇到问题。

您将需要使用$(sector)从DOM创建jQuery对象,例如$('#search-form')根据您的代码,您可能应该将onsubmit更改为

onsubmit="return doSearch('#search-form','#text_cautat');"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM