创建一个基于搜索输入打开新网址的搜索表单

时间:2015-08-20 21:23:06

标签: javascript html twitter-bootstrap url search

我使用bootstrap创建了一个表单,你可以在下面看到它。  form

我试图找出如何让用户输入一个值(5位数)以及当他们点击"搜索"按钮将打开一个新窗口,显示搜索结果。网址取决于输入搜索栏的5位数字。为所有搜索更改的网址唯一部分是搜索框中添加的数字。

http://monkey=13857&red

例如,他们在搜索栏中输入13857,当他们点击"搜索"一个新的寡妇开始将他们重定向到http://monkey=13857&red。我是javascript的新手,但我想我会用它来完成这项任务 - 任何帮助都将非常感激。谢谢。

- 更新 - 嗨mwilson(以及所有帮助过这么快的人),我实现了代码(谢谢你的帮助),看起来我的代码并没有将搜索数字添加到网址中。这是我的表单代码

HTML

<form>
<div class="form-group">
<label for="exampleInputEmail1">WorkFlow by Request ID</label>
<input type="text" class="form-control" id="search" placeholder="Request #">
</div>
<button type="submit" class="btn btn-default" id="WFF">Submit</button>
</form>  

的JavaScript

    $('#WFF').on('click', function () {
    var searchInput = $('#search').text();
    var url = "http://monkey=" + searchInput + "&red";
    window.open(url);
});

如果我在搜索框中输入12345并点击提交按钮,则会打开该网站,但未输入搜索结果 - http://monkey=&red而不是http://monkey=12345&red

4 个答案:

答案 0 :(得分:4)

您可以使用window.open(<url>)启动该窗口。然后,只需要构建正确的url字符串,可以通过创建保存搜索值和url的变量来完成。根据需要构建它,然后将其传递给window.open(<url>)函数并进行设置。

<强> JQuery的

$('#btnSearch').on('click', function () {
    var searchInput = $('#textBoxEl').val();
    var url = "http://monkey=" + searchInput + "&red";
    window.open(url);
});

只是JavaScript

var button = document.getElementById("btnSearch");

button.onclick = function () {
    var text = document.getElementById("textBoxEl").value;
    window.open("http://monkey=" + text + "&red");
}

答案 1 :(得分:0)

写一个像这样的javascript函数,

function search()
{
var searchInput = document.getElementById('search').text();
var url = "http://monkey=" + searchInput + "&red";
window.open(url);
}

并调用函数

<input type="button" onclick="search()"

答案 2 :(得分:0)

也许您确实需要以这种方式构建URL ...但通常表单是“提交”。接收表单可以访问POST给它的数据,您可以使用它来运行搜索。 您可以通过设置target = _blank。

形式在新窗口中打开
<form target="_blank" action="monkey.html">
     <!-- your form here -->
     <input type="submit" value="Submit"/>
</form>

答案 3 :(得分:0)

首先导入JQuery&amp; 试试这段代码

public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
    for (int startByte = 0; startByte < scanRecord.length; startByte++) {
        if (scanRecord.length-startByte > 19) { // need at least 19 bytes for Eddystone-UID
            // Check that this has the right pattern needed for this to be Eddystone-UID
            if (scanRecord[startByte+0] == 0xaa && scanRecord[startByte+1] == 0xfe &&
                    scanRecord[startByte+2] == 0x00) {
                // This is an Eddystone-UID beacon.
                byte[] namespaceIdentifierBytes = Arrays.copyOfRange(scanRecord, startByte+4, startByte+13);
                byte[] instanceIdentifierBytes = Arrays.copyOfRange(scanRecord, startByte+14, startByte+19);
                // TODO: do something with the above identifiers here
            }
        }

    }
}
  

使用$('#WFF').on('click', function () { var searchInput = $('#search').val(); var url = "http://monkey=" + searchInput + "&red"; window.open(url); }); 代替val()