将变量放入window.location.replace()中
Place variable inside window.location.replace()
我试图使jquery打开一个页面与url
search.php?city=cityname
其中cityname是用户从输入中选择的值。这段代码总是打开以下url
search.php?city=cityname
由于某些原因,城市名称没有被拾取。
<input id="city" placeholder="<?php echo $_GET['city'] ?>">
这是JavaScript。
<script>
$('#city').on('blur', function() {
var cityname = $('#city').val;
window.location.replace("search.php?city=cityname");
});
</script>
两个错误
-
val
函数不被调用,否则该函数将被赋值给cityname
变量 - 变量需要与
+
操作符连接
$('#city').on('blur', function () {
var cityname = $('#city').val(); // Invoke the function here
window.location.replace("search.php?city=" + cityname); // Concat the variable value here
});
相关文章:
- 在AJAX中使用window.location.replace'成功'回调
- 后退按钮在 window.location.replace(href);之后失败
- 我能确定这个页面赢了吗;如果我使用`location.replace`来更改哈希,则不会重新加载
- How to do "HTML FORM action=location.replace('nextp
- window.location.replace.继续重新加载页面,如何停止
- window.location.replace或href时Web会话丢失
- “拒绝影子本机属性的权限”使用 location.replace 在 JavaScript 中重定向
- 如何使 window.location.replace 在 IE 9 中工作
- 使用 window.location.replace 但页面仍在浏览器的内存中
- JavaScript Location.replace 不起作用
- 使用 window.location.href.replace + 单击按钮
- window.location.replace :权限被拒绝和未捕获的异常
- window.location.replace() 在窗体中不起作用
- Javascript 密码验证窗口.location.replace.
- 如何在window.location.replace.之后保持相同的表单
- 使用window.location.replace重定向不起作用
- window.location.replace不起作用
- window.location.replace在客户端不起作用
- 如何覆盖JavaScript中的location.assign或location.replace函数
- 如果重新加载了同一页,window.location.replace将不起作用