下拉元素选择和 JS 调用不起作用
Dropdown element selection and JS call is not working
我有一个HTML页面,里面有一个下拉菜单和一个按钮。
选择一个元素后,按下按钮,我调用函数按照以下代码进行操作:
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Custom dropdown</title>
<link rel="stylesheet" href="cssdropdown/style.css">
</head>
<body>
<h3>Restart any service from here</h3>
<!--form method="post" action="example_new.php"-->
<span class="custom-dropdown">
<select name="restart-component" id="restart-component">
<option value=0>Data 1</option>
<option value=1>Data 2</option>
</select>
</span>
<button id="submit" name="submit" style="background-color:#1abc9c padding-left: 2cm;" onclick='goToRestart();'>Restart</button>
<p>Legend</p>
<br/>
<script type="text/javascript">
function goToRestart() {
alert ("into js");
var url;
var location = document.getElementById("restart-component").selectedIndex;
if (location == 1) {
url = 'http://www.page.com/west';
}
else if (location == 2) {
url = 'http://www.page.com/land';
}
#window.location.href=url;
alert(url)
}
</script>
<script src='http://ajax.googleapis.com/ajax/libs/prototype/1.7.1/prototype.js'></script>
<script src="jsdropdown/index.js"></script>
</body>
</html>
但是我的JavaScript函数从未被调用过。我错过了什么吗?请帮忙。
将
函数包装在onLoad()
中或更改脚本,如下所示
<script type="text/javascript">
(function() {
// your page initialization code here
// the DOM will be available here
function goToRestart() {
alert("into js");
var url;
var location = document.getElementById("restart-component").selectedIndex;
if (location == 1) {
url = 'http://www.page.com/west';
} else if (location == 2) {
url = 'http://www.page.com/land';
}
window.location.href = url;
alert(url);
}
})();
</script>
先尝试注释此行
//#window.location.href=url;
<script type="text/javascript">
function goToRestart() {
alert ("into js");
var url='empty';
var location = document.getElementById("restart-component").selectedIndex;
if (location == 1) {
url = 'http://www.page.com/west';
}
else if (location == 2) {
url = 'http://www.page.com/land';
}
//#window.location.href=url; //this line
alert(url)
}
</script>
当然,您会喜欢使用"尝试捕获"
脚本上存在语法错误。尝试:
function goToRestart() {
alert("into js");
var url;
var location = document.getElementById("restart-component").selectedIndex;
if (location == 0) {
url = 'http://www.page.com/west';
} else if (location == 1) {
url = 'http://www.page.com/land';
}
//#window.location.href=url; line with syntax error on the character #
window.location.href = url;
alert(url);
然后将函数的调用从 onclick='goToRestart();'
更改为仅onclick='goToRestart()'
希望对你有帮助
相关文章:
- 避免在来自HTML的Scala.js调用中重复包名称
- 如何从角度 js 调用 javascript 方法
- Odoo销售点如何访问模型并使用JS调用方法
- 存储在 2D 数组中的 js 调用函数
- 使用 Vue.js 调用 Node.js 服务器
- 有没有一种方法可以在windows上使用Grunt.js调用gitshell中的命令
- 使用助手METEOR-JS调用Facebook照片
- 当 Node.js 调用 HTML 时,外部 Javascript 和 CSS 不会对 HTML 产生影响
- 使用node.js调用webservice时需要一些帮助
- 如何调试JS调用
- 当控制台不在't在chrome中打开,有些项目不再可点击(有些js调用停止工作)
- 如何使用monk/node.js调用mongodb函数
- Marketo monchkin.js调用间歇性无响应.(CORS)
- 如何在AngularJS ngview中加载图像后执行foundation js调用
- file_get_contents在动态 js 调用中不起作用
- 从 JS 调用 IE Js 调试器
- 在 selectedOptions 属性中挖空 JS 调用函数
- JS调用方法,为什么不直接使用括号
- Angular JS调用Sinatra不返回数据
- 如何从 HTML 文档中的外部 JS 调用/启动此函数