-2

我需要用 jquery 处理这段代码。

我需要获取作为关键字的文本框的值,点击链接将重定向。

所以我很难从文本框中获取该值,然后运行重定向。这是这条线 $("#serviceorder").val("keyword");

HTML 代码:

<html>
<input id="serviceorder" type="text" data-bind="value: keyword" />
<a href="" id="g-search-button"></a>
</html>

JS代码:

$("#g-search-button").click(function (event) {
                    event.preventDefault();
                        $("#serviceorder").val("keyword");      
                    var keyword = event.currentTarget.value;

                if (keyword.length > 0) {
                    window.location.href = "URL + encodeURIComponent(keyword);
                    return false;               
                }
            });
4

1 回答 1

0

如果您想被重定向到一个 url,例如:https ://stackoverflow.com/keywordHere

    $(document).ready(function(){
    $("#g-search-button").click(function (event) {
    var URL='http://stackoverflow.com';//change this as you want
                    event.preventDefault();
                    var keyword =  $("#serviceorder").val();
                    if (keyword.length > 0) {
                        window.location.href = URL +'/'+ encodeURIComponent(keyword);
                    }
                });
    });
于 2013-09-06T01:07:45.120 回答