当在<选择>-HTML

reload page when item selected in <select> - HTML

本文关键字:-HTML gt 选择 当在 lt      更新时间:2023-09-26

关于<select></select> 的使用,我有一个小问题

我通过请求从服务器接收数据。对于这个请求,我使用了shtml。

现在我想要的是,当用户在deselectbox中选择一个项目时,页面会从服务器获取该项目的数据。对服务器的请求可能类似于%! tcp-connections

因此,我认为我必须使用JavaScript或类似的东西刷新页面。有人能告诉我怎么做吗?

HTML:

<select id="select">
    <option selected>Default</option>
    <option value="refresh">Refresh</option>
</select>

JavaScript:

function onchange(e) {
    if (e.currentTarget.value === 'refresh') {
        window.location.reload();
    }
}
document.getElementById('select').addEventListener('change', onchange);

演示:http://jsfiddle.net/w425208t/

使用window.location.href = yourUrl;它将"重定向"到url为yourUrl的页面

您可以使用以下任何一种:

window.location.reload(false); 
// If we needed to pull the document from
//  the web-server again (such as where the document contents
//  change dynamically) we would pass the argument as 'true'.
//i.e. 'true' will force the page to reload from the server. 'false' will reload from cache, if available.
or 
location.reload(); 
or 
window.location.replace(window.location.pathname);