如何在快递js中将URL中的选定值显示为字符串?

时间:2017-11-30 11:52:33

标签: javascript node.js express pug

我使用Pug作为视图文件,任务是从select下拉列表中选择值,然后将其传递给url。我收到了错误" / users?sortby = [object NodeList] "。

请在下面找到pug文件来源 `

doctype html
html
    head
        script(type='text/javascript' src="/javascripts/script.js")
        link(rel="stylesheet", href="/stylesheets/style.css")
        h4=page_title
    body
        span Sort by
        select(name = 'sorting' id = 'sort' onchange = 'onSorting()')
            option(value = 'asc') Asc
            option(value = 'desc') Desc
            option(value = 'top 5') Top 5
            option(value = 'bottom 5') Bottom 5
        table#desc
            tr
                th ID
                th Name
                th Email
            each users in userdetail
                tr
                    td= users.id
                    td= users.name
                    td= users.email
html

`

我的script.js来源如下 `

function onSorting(){
    var sortby = document.getElementsByName('sorting');
    window.location = "/users?sortby=" + sortby;
}

`

我得到了输出网址如下 / users?sortby = [object NodeList]

请有人帮助我清除它。

1 个答案:

答案 0 :(得分:0)

它可能会有所帮助

    function onSorting(){
        var sortby = document.getElementById('sorting');
         alert(sortby.value)
        //window.location = "/users?sortby=" + sortby.value;
    }
<select id="sorting" onchange = 'onSorting()'><option value="asc">asc</option><option value="desc">desc</option>

</select>