繁体   English   中英

调用Javascript函数不起作用

[英]Javascript functions not working when called

我在php文件中定义了2个javascript函数

<script type="javascript">
function inviaRichiesta(){
    //some data elaborations
    location.href = '../index.php'; //page redirect       
}

function caricaDati(){
    var ele = document.getElementById("int").options[document.getElementById("int").selectedIndex].text;
    document.getElementById("tip").value = <?php echo $_SESSION['caricato']['tipologia']; ?>
}
</script>

但是当我更改选择菜单中的值时它们似乎都不起作用

  <select name="intervento" onchange="inviaRichiesta()" id="int">

我需要的是在选择菜单中做出选择后将浏览器重定向到页面。 有什么线索吗?

控制台输出:

> [12:09:07.771] ReferenceError: inviaRichiesta is not defined @
> http://localhost/tirocinio/view/inserimento_dati_intervento.php:1

解:

要重定向到其他页面,您不应使用location.href ,而应使用“ window.location”。 只需更换并尝试。

您也可以尝试

onchange =“ window.location ='.. / index.php'”

location.href -href属性返回当前页面的整个URL。

window.location -window.location对象可用于获取当前页面地址(URL)并将浏览器重定向到新页面。

参考: http//www.w3schools.com/jsref/prop_loc_href.asp

http://www.w3schools.com/js/js_window_location.asp

演示版

在更改页面上重定向到URL

window.location.assign("http://www.w3schools.com");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM