2016-11-25 2 views
0

誰かがステージやステージのようなアルバムのタイトルを入力すると、ステージのイド。 IDはギャップを持つことができないので、私はどうにかまずすべての大文字を小さく変換しなければならないし、自分のIDと一致するスペースも削除しなければならない。私はそれを修正する方法について誰でも良いヒントを持っていますか?入力ではあるがIDではなく、ターゲットとして使用する

<div class="ram" id="stage"> 
     <details> 
      <summary>Click here to make a song selection!</summary> 
      <a href="../AvengedSevenfold/stage/TheStage.html"><li>The Stage</li></a> 
      <a href="../AvengedSevenfold/stage/Paradigm.html"><li>Paradigm</li></a> 
      <a href="../AvengedSevenfold/stage/Sunny.html"><li>Sunny Disposition</li></a> 
      <a href="../AvengedSevenfold/stage/Damn.html"><li>God Damn</li></a> 
      <a href="../AvengedSevenfold/stage/creating.html"><li>Creating God</li></a> 
      <a href="../AvengedSevenfold/stage/angels.html"><li>Angels</li></a> 
      <a href="../AvengedSevenfold/stage/simulation.html"><li>Simulation</li></a> 
      <a href="../AvengedSevenfold/stage/higher.html"><li>Higher</li></a> 
      <a href="../AvengedSevenfold/stage/roman.html"><li>Roman Sky</li></a> 
      <a href="../AvengedSevenfold/stage/fermi.html"><li>Fermi Paradox</li></a> 
      <a href="../AvengedSevenfold/stage/exist.html"><li>Exist</li></a> 
     </details> 
     <img src="../bilder/The-Stage-Album-Cover-sept8_CMYK.jpg" alt="The Stage album"> 
     </div> 

function main() { 
    var $body = $("body").hide(); 
    $("body").fadeIn(1000); 
    $('summary').each(function(){ 
    $(this).nextAll().wrapAll('<div class="slide"></div>'); 
    }); 
    $('details').attr('open','').find('.slide').css('display','none'); 
    $('summary').click(function(e) { 
    e.preventDefault(); 
    $(this).siblings('div.slide').slideToggle(function(){ 
    $(this).parent('details').toggleClass('open');}); 
    }); 
    } 
$(document).ready(main); 


function sendToPage() { 
    var searched = $("#search").val().toLowerCase(); 
    console.log(searched) 
    $('html, body').animate({ 
    scrollTop: $("#" + searched).offset().top 
    }, 1000); 
} 

function match(elem) { 
    var validSearch = [ 
    "The Stage", 
    "Hail to the King", 
    "Nightmare", 
    "Avenged Sevenfold", 
    "City of Evil", 
    "Waking the Fallen", 
    "Sounding the Seventh Trumpet"]; 

    /** make search case insentive **/ 
    var searchKeys = []; 
    for(c=0;c<validSearch.length;c++) { 
    searchKeys[c] = validSearch[c].toLowerCase(); 
    } 
    /** search the input **/ 
    var index = searchKeys.indexOf(elem.value.toLowerCase()); 

    /** if not matched **/ 
    if(index == -1) { 
    window.alert("Album does not exist. Please try again"); 
    } 
    else { 
    var album = validSearch[index]; 
    window.alert("FOUND IT! " + album); 
    } 
} 

答えて

0

match関数は下の2つの主な変更点があります。今

  • アレイsearchKeysを全て除去スペース、ならびに下部ケースを有しています。
  • それはそれはスペースのない別の小文字の文字列とスペースなしと小文字の文字列を比較し、入力文字列

からすべてのスペースを削除します。

function match(elem) { 
    var validSearch = [ 
    "The Stage", 
    "Hail to the King", 
    "Nightmare", 
    "Avenged Sevenfold", 
    "City of Evil", 
    "Waking the Fallen", 
    "Sounding the Seventh Trumpet"]; 

    /** make search case insentive **/ 
    var searchKeys = []; 
    for(c=0;c<validSearch.length;c++) { 
    searchKeys[c] = validSearch[c].toLowerCase(); 
    // we also remove all spaces in the strings 
    while (searchKeys[c].indexOf(' ') > -1) searchKeys[c] = searchKeys[c].replace(' ',''); 
    } 
    /** search the input **/ 
    // remove all spaces from the search string 
    searchString = elem.value.toLowerCase(); 
    while (searchString.indexOf(' ','') > -1) searchString = searchString.replace(' ',''); 
    var index = searchKeys.indexOf(searchString); 

    /** if not matched **/ 
    if(index == -1) { 
    window.alert("Album does not exist. Please try again"); 
    } 
    else { 
    var album = validSearch[index]; // Album full name 
    var anchor = searchKeys[index]; // Album lowercase full name 
    while (anchor.indexOf(' ')>-1) anchor = anchor.replace(' ',''); // we remove the spaces 
    window.alert("FOUND IT! " + album + " will now scroll to " + anchor); 
    window.location.href = '#'+anchor; // scroll to the given object 
    } 
} 

Working fiddle

+0

それは動作しません。 – Adam

+0

あなたの例では、見つかったアルバムをあなたのID(小文字、小文字、小文字、小文字、小文字、小文字、小文字、スペースなし)、あなたはあなたの問題が(スペースを無視して)アルバムを見つけることでもあったとは言いませんでした。質問を投稿するときは、あなたが期待している答えが何であるかを明確に指定してください。問題を解決するには、 'elem'と' validSearch'の両方からすべてのスペースを削除することができます。私は答えを更新します。 –

+0

申し訳ありませんが、私の悪い!しかし、それは私が「私はステージやステージのようなアルバムのタイトルを誰かが入力すると、ステージのIDを持つセクションに行くよ – Adam

0

以下は小文字に「ステージ」または「ステージ」に変換し、ホワイトスペースを削除します:

var str = "The Stage"; 
var res = str.toLowerCase(); 
res = res.replace(/\s/g, ''); 

はあなたを与える:thestage

関連する問題