Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中将变量传递给href?_Javascript_Href_Location Href - Fatal编程技术网

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

如何在javascript中将变量传递给href?,javascript,href,location-href,Javascript,Href,Location Href,如何在此处传递此变量值?下面的代码不工作。关于Stackoverflow的所有其他讨论都不清楚 <script type="text/javascript"> function check() { var dist = document.getElementById('value'); if (dist!=""){ window.location.href="distric

如何在此处传递此变量值?下面的代码不工作。关于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>

函数检查()
{
var dist=document.getElementById('value');
如果(距离!=“”){
window.location.href=“district.php?dist=“+dist;
}
其他的
警报('Oops.!!');
}
我的HTML代码是:

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

您已获取字段的
,当前正在使用DOM对象

使用

或 使用

而不是

if (dist!=""){
     window.location.href="district.php?dist="+dist;

在将整个对象作为
文档传递给URL时,必须使用
.value
获取字段值。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.!!');
}
试试这个:

function check() {
    var dist = document.getElementById('value').value;

    if (dist) {
        window.location.href = "district.php?dist=" + dist;
    } else {
        alert('Oops.!!');
    }
}
var dist = document.getElementById('value').value;
if (dist != "") {
 window.location.href="district.php?dist="+dist;
}
试试这个:

function check() {
    var dist = document.getElementById('value').value;

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

你必须对你的函数作一些修正

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

我使用了一种非常不同的方法来解决这个问题。我在客户端中设置浏览器cookie,这些cookie在我设置
window.location.href
后一秒钟过期

这比在URL中嵌入参数更安全

服务器将参数作为cookie接收,浏览器在发送cookie后立即删除这些cookie

const expires=新日期(Date.now()+1000).toutString()
document.cookie=`oauth username=user123;expires=${expires}`
window.location.href=`https:foo.com/oauth/google/link`

您可能还需要对其进行编码。感谢您的帮助,PrateekI没有足够的声誉来投票支持。这就是为什么不投票:)@Joel,投票表决你的问题很快你就会有足够的声望来投票表决。就我而言,帮助社区成员比帮助名誉点更重要。玛丽·马斯梅里·马斯先生:)谢谢