将变量放置在window.location.replace()内

用户379888

我正在尝试使jQuery打开带有URL的页面

search.php?city=cityname

其中cityname是用户从输入中选择的值。此代码始终打开以下网址,

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>
杜沙尔

两个错误

  1. val函数不会被调用,否则函数将被分配给cityname变量
  2. 该变量需要与+运算符连接

代码:

$('#city').on('blur', function () {
    var cityname = $('#city').val(); // Invoke the function here
    window.location.replace("search.php?city=" + cityname); // Concat the variable value here
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在window.location.replace中使用参数

window.location.replace-POST版本?

window.location.replace奇怪的行为

反应版本的 window.location.replace()?

window.location =和window.location.replace()有什么区别?

window.location.assign()和window.location.replace()之间的区别

如何将变量赋给window.location

试图将变量传递给window.location.hash

将 html / javascript 变量插入 URL window.location

如何将查询字符串传递给window.location.replace()?

是否可以捕获window.location.replace事件?

Javascript window.location.replace只是重新加载了此页面

关于window.location.href.replace的各种可能性

如何在jquery或javascript中使用window.location.replace

window.location.replace无法正常工作Laravel Ajax

本地存储和 window.location.href.replace

window.location.href thymeleaf 多个变量

已解决:将 html / javascript 变量插入 URL window.location

JavaScipt window.location

如果网址包含“#”,则不会触发window.location.replace(document.URL)在最后

window.location.replace在本地主机上不起作用

如何在AngularJS单元测试中模拟$ window.location.replace?

如果重新加载同一页面,则window.location.replace无法正常工作

如何使用window.location.href.replace切换href网址中的语言?

Phonegap-在我使用window.location.replace离开索引范围后处理推送通知

电子的remote.getGlobal()在window.location.replace()之后返回“未定义”

angularjs:在 window.location.replace 更改模板之前更新 ng-show

使用window.location.replace(“ xx”)重定向页面;根据条件

window.location.replace(); 不是在单击按钮时重定向页面