javascript - 如何在javascript中将变量传递给href?

标签 javascript href location-href

这里如何传递这个变量值?下面的代码不起作用。 Stackoverflow 上的所有其他讨论都不清楚。

<script type="text/javascript">
        function check()
        {
            var dist = document.getElementById('value');
            if (dist!=""){
                window.location.href="district.php?dist="+dist;
            }
            else
               alert('Oops.!!');
        }
</script>

我的 HTML 代码是:

<select id="value" name="dist" onchange="return check()">

最佳答案

您必须使用 .value 获取字段值,因为您将整个对象传递给 URL,因为 document.getElementbyId('value') 返回整个字段对象。

var dist = document.getElementById('value').value;

所以你的函数应该是这样的

function check() {
    var dist = document.getElementById('value').value; // change here
    if (dist != "") {
        window.location.href = "district.php?dist=" + dist;
    } else
        alert('Oops.!!');
}

关于javascript - 如何在javascript中将变量传递给href?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20769299/

相关文章:

javascript - Aurelia - 值输入取决于另一个输入

javascript - JavaScript中currentTarget属性和目标属性的确切区别是什么

javascript - Chrome 扩展程序的指标

javascript - 旧代码 : window. Parent.location.href ="https:/home"

javascript - 使用 window.location.href 或 .assign 或 .reload 时等待时间很长

javascript - 正则表达式问题(长字符串)

javascript - 找到具有给定标签的第一个链接并打开它(用户脚本)

javascript - HREF 的修改超出了我的控制范围...?

javascript - 使用用户 ID 链接到 Instagram 个人资料

javascript - 如何从 iFrame 内部重定向?