0

これは非常に簡単で、同様の質問を見たことがありますが、これが機能しない理由を理解するのに役立つものはありません。currentPageが orに設定されているときにこれがキャッチされない理由を誰か教えてもらえませんfaqcontact?

<script type="text/javascript">
                function init() {
                document.getElementById('searchSubmit').onclick=function(){
                        var uriArgs = window.location.search;
                        var currentPage = uriArgs.match(/page=?(\w*)/);
                        if ( (currentPage == null) || (currentPage == 'faq') || (currentPage == 'contact') ) {
                                currentPage = "index";
                                document.getElementById('searchHidden').value = currentPage;
                        }
                        else {
                                document.getElementById('searchHidden').value = currentPage[1];
                        }
                }
                }
                window.onload=init;
        </script>

ポップアップにアラートを設定して、それが適切に設定されているかどうかを確認できるfaqようにしましたが、ステートメントがそれをキャッチしていcontactない理由がわかりません。if

ありがとうございます!

4

2 に答える 2

2

一致する場合はString.match、配列のインデックス1にあるキャプチャグループを返します。例:

> 'page=sdfsfsdf'.match(/page=?(\w*)/)
["page=sdfsfsdf", "sdfsfsdf"]

したがって、によって返される配列の内部を調べる必要がありますmatch(そうではないと仮定しますnull)。

if (currentPage == null || currentPage[1] == 'faq' || currentPage[1] == 'contact') {
    /* ... */       
}
于 2012-10-04T18:50:47.220 に答える
0

Your problem is not with the if-statement, but with the result of the match method: It returns an array of the match and the matched groups - you want to compare to the first matched group.

var match = window.location.search = uriArgs.match(/page=?(\w*)/),
    currentPage = false;
if (match != null)
    currentPage = match[1];

if ( !currentPage || currentPage=='faq' || currentPage=='contact' ) {
    // do something
}
于 2012-10-04T18:52:21.187 に答える