2017-10-04 7 views
0

私は単純なjavascriptやphpやフィールドに完全な16桁の数字をコピーして貼り付けることができますが、最初の6桁を使ってURLの最後に追加します。これは私がこれまで持っているものであるjavascript動的リンクは、短縮された値を持つ入力に基づいていますか?

...

<html> 

<head> 
    <title>BIN Search</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 

    <script type="text/javascript"> 
     $(document).ready(function() { 

      $('#button').click(function(e) { 
       var inputvalue = $("#input").val(); 
       window.location.replace(" https://bincheck.org/" + inputvalue); 

      }); 
     }); 
    </script> 
</head> 

<body> 
    <input class="form-control" id="bin" type="number" placeholder="Enter card" autofocus=""> 
    <button type="button" id="button">Search</button> 
</body> 

</html> 

答えて

0

jQueryのval()関数は(el.valueとしてこれを公開する入力要素el)入力要素の内容を表す文字列を返します。

文字列インスタンスで使用できる操作が多数あります。あなたがしたいことは、文字列のサブセクションを取るか、またはsubstringを省略することです。 substring method at MDNに関するドキュメントをご覧ください。あなたはこの線に沿って何かにあなたのコールバックを変更したいあなたのケースで

function(e) { 
    var inputvalue = $("#input").val(); 
    window.location.replace("https://bincheck.org/" + inputvalue.substring(0, 6)); 
} 
+0

それは間違いなく近いです私が持っていたものよりも、しかし、それはちょうどページを直接持っている必要があるURLとのダイアログボックスを開くようだ...任意のアイデア? – Josh

0

$("#input")が無効であること - bin - 入力は、別のIDを持っています。あなたはチャンクに入力したサブストリングを使用することができます。

$(document).ready(function() { 
 

 
      $('#button').click(function(e) { 
 
       var inputvalue = $("#bin").val().substring(0, 6); 
 
       // console.log(" https://bincheck.org/" + inputvalue); 
 
       window.location.replace(" https://bincheck.org/" + inputvalue); 
 
      }); 
 
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input class="form-control" id="bin" type="number" placeholder="Enter card" autofocus=""> 
 
<button type="button" id="button">Search</button>

+0

URLを持つダイアログボックスの代わりに、URLを取得してブラウザにロードする方法はありますか? – Josh

+0

'window.location.replace(" https://bincheck.org/456456 ");'は現在のドキュメントを新しいものに置き換えます。フォームなどで変更をサブミットしなかった場合、ダイアログボックスが表示されることがあります。問題を説明するためにjsbinのような別のページで例を共有できますか?私は別のウィンドウで実行する場合、このコードは正常に動作します –

0

/**あなたの要件ごとにコードの変更:**/

<html> 
    <head> 
     <title>BIN Search</title> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 

     <script type="text/javascript"> 
      $(document).ready(function() { 

       $('#button').click(function(e) { 
        var inputvalue = $.trim($("#bin").val()); 
        if(inputvalue.length == 16){ 
          window.location.replace(" https://bincheck.org/" + inputvalue.substring(0,6)); 
        } 
       }); 
      }); 
     </script> 
    </head> 

    <body> 
     <input class="form-control" id="bin" type="number" placeholder="Enter card" autofocus=""> 
     <button type="button" id="button">Search</button> 
    </body> 
</html> 
関連する問題