檢測文本字段中的某個模式,然后在兩個單選按鈕之間切換

[英]detect a certain pattern in a text field, then switch between two radiobuttons


I have two radio buttons connected to a search-field that controls what type of search the user wants.
One for regular keyword search, and another for license plate search.

我有兩個單選按鈕連接到搜索字段,控制用戶想要的搜索類型。一個用於常規關鍵字搜索,另一個用於車牌搜索。

The "keyword" radio button is pre-selected, but when a license plate pattern is detected, I would like to have it switched over to "licence plate" automatically using jQuery if that's possible...

“關鍵字”單選按鈕是預選的,但是當檢測到車牌模式時,如果可能的話,我想讓它自動切換到“車牌”使用jQuery ...

The license plate pattern will allways start with two letters (A-Z), followed by five numbers (0-9) like this: "AZ12345". Possibly sepparated with a space like this: "ZA 98765".

車牌模式將始終以兩個字母(A-Z)開頭,然后是五個數字(0-9),如下所示:“AZ12345”。可能會像這樣的空間:“ZA 98765”。

Form method is _GET.

表單方法是_GET。

2 个解决方案

#1


3  

Following your instructions you can use this pattern to detect license plates

按照說明操作,您可以使用此模式檢測車牌

 [A-Za-z]{2}\s?\d{5}

If you want to match only plates (eg not liasdjfaAG 00212lkfla) use ^[A-Za-z]{2}\s?\d{5}$ instead.

如果你只想匹配板塊(例如不是liasdjfaAG 00212lkfla),請使用^ [A-Za-z] {2} \ s?\ d {5} $。

You can use the textchanged event (look at this answer), then you can change the "checked" state of the radio button if the pattern is matched.

您可以使用textchanged事件(查看此答案),然后如果模式匹配,您可以更改單選按鈕的“已檢查”狀態。

EDIT:

編輯:

Implemented in jsfiddle, basically the code looks like this

在jsfiddle中實現,基本上代碼看起來像這樣

var rege = /^[A-Za-z]{2}\s?\d{5}$/;

$('#inpt').keyup(function(event) {
    if (rege.test($('#inpt').val())) {
        $('#plate').attr('checked', true);
    } else {
        $('#nonplate').attr('checked', true);
    }
});

with an html like this:

用這樣的HTML:

<input type="text" id="inpt"/><br/>
<input type="radio" name="group1" id="plate" value="Plate"> Plate<br>
<input type="radio" name="group1" id="nonplate" value="NotPlate"> Not Plate

EDIT 2:

編輯2:

In response to your request I suggest you to read this question. Basically with setInterval() you can check the state of the input every 1 or 2 seconds to select the correct radio button. You might shorten this interval as well (setting it to ~100 ms)

根據您的要求,我建議您閱讀此問題。基本上使用setInterval(),您可以每隔1或2秒檢查輸入的狀態,以選擇正確的單選按鈕。您也可以縮短此間隔(將其設置為~100 ms)

#2


0  

I believe what you need is this:

我相信你需要的是這個:

function checkPlate($s) {
  $match = false;
  $s = strtoupper($s);
  $s = str_replace(" ","",$s);
  if (strlen($s) == 7) {
    preg_match(/[A-Z]{2}[0-9]{5}/, $s, $matches);
    if (count($matches) == 1) {$match = true;}
  }
  return $match;
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/08/27/7209d28d584c64c28fc311620ee160f9.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com