window.location 无法使用 get element by id 来工作

window.location not working by using get element by id

本文关键字:by element id 工作 get location window      更新时间:2023-09-26

您好,我在下面的代码中提出了一个新问题,在下面的代码中window.location不起作用,尽管window.alert工作正常,显示了我想要的 url 和我为其编写此代码的emailid值。需要一点帮助,我是这个领域的新手。你的一点帮助会让我学得更好。提前谢谢。

<html> 
<form> 
<input id="c" type="hidden" value="http://mydomain.com"/>
<input id="d" type="hidden" value="/somefile.php?emailid=/> 
<h5>Your Email adress</h5>
<br>
<input id="e" type="text"><br>
<input type="button" value="submit" onclick="display_alert();" /> 
</form>
<script lang="javascript"> 
function display_alert() 
{ var dn = document.getElementById('d'); var cn = document.getElementById('c'); 
  var fn = document.getElementById('a');
  var ln = document.getElementById('e'); }
 location = "cn.value + dn.value + ln.value";
 </script> 
 </html>
location

是一个函数。用=给它值,不要用()来调用它。


或者完全消除对JS的不必要的依赖:

<form action="http://mydomain.com/somefile.php">
<label>Your Email address
<input name="emailid"></label>
<input type="submit" value="submit"> 
</form>

针对正在编辑的问题进行更新:

如果要连接存储在变量中的值,则需要使用实际变量,而不是将代码连接它们放在字符串文本中。

按如下方式修改代码,因为位置是属性

window.location = cn.value + dn.value + ln.value;