如何在哈希中设置输入值?

时间:2013-11-09 11:40:19

标签: javascript knockout.js url-routing sammy.js

 self.search = function() {
        searchRes = $('#search_field').val();
        location.hash = "search" + "/" + searchRes;
    };     
 Sammy(function () {
                    this.get('#search',':searchRes', function() {
                    this.param.searchRes = $('#search_field').val();
                        $('.inside').each(function() {
                            console.log(this.params);
                            if ($(this).text().search($('#search_field').val()) == -1) {
                                $(this).parent().addClass('blind');
                            }
                        });
                    });
                }).run();

我在url中设置了输入值,但是我无法获得需要的哈希值来传递get函数。我收到这样的错误:

  

/ #search / resultError {message:“404 Not Found get /#search / result”}

1 个答案:

答案 0 :(得分:0)

根据SammyJS docs您的路线必须定义如下:

this.get('#/search/:searchRes', function() { 
    var searchRes= this.params['searchRes'];    
});

由于您可以从param属性中获取搜索条件,我建议使用此示例:

var searchRes = this.params['searchRes']; 

SammyJS对象提供了redirect方法。

self.search = function() {
    searchRes = $('#search_field').val();
    app.redirect("#/search" + "/" + searchRes;);          
};   

我希望它有所帮助。