简体   繁体   English

调用Javascript函数不起作用

[英]Javascript functions not working when called

I defined 2 javascript functions in a php file 我在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>

but none of them seems to work when i change the value inside the select menu 但是当我更改选择菜单中的值时它们似乎都不起作用

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

what i need is to redirect the browser to a page when a choise in the select menu is made. 我需要的是在选择菜单中做出选择后将浏览器重定向到页面。 any clue? 有什么线索吗?

Console output: 控制台输出:

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

Solution: 解:

For redirecting to some other page you should not use location.href you should use "window.location". 要重定向到其他页面,您不应使用location.href ,而应使用“ window.location”。 Just replace and try. 只需更换并尝试。

You can try this as well, 您也可以尝试

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

location.href - The href property returns the entire URL of the current page. location.href -href属性返回当前页面的整个URL。

window.location - The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page. window.location -window.location对象可用于获取当前页面地址(URL)并将浏览器重定向到新页面。

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

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

Demo 演示版

on change page is redirected to URL 在更改页面上重定向到URL

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

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

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