2017-10-04 79 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公开此为el.value)。

有很多可用于字符串实例的操作。你想要做的是取一个字符串的小节,或简称为substring。请参阅关于substring method at MDN的一些文档。

在你想你的回调沿着这条线更改为您的情况:

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

这绝对是更接近比我有,但它似乎打开一个对话框,我需要有网页的网址直接...任何想法? – Josh

0

注意$("#input")是无效的 - 输入有另一个ID - bin。您可以使用字符串来块输入:

$(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的对话框,有没有办法让网址刚刚在浏览器中加载? – 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>