Javascript-函数未定义(onChange不起作用)
Javascript - Function is not defined (onChange doesn't work)
我在javascript文件中使用HTML文件中标记中的onChange()命令调用一个名为getPrice()的函数,但无论我做了什么或修复了什么,它仍然没有显示价格。我做了很多研究,但似乎找不到任何正确的解决方案。我们将非常感谢您的帮助。
这是我的HTML代码:
<body>
<form action="#" name="ITSbookform" id="ITSform">
<fieldset>
<legend>Into The Storm Booking</legend>
<label>Choose your cinema</label><br>
<select id="selectedCinema">
<option>--Select--</option>
<option value="maximaCinema">Maxima Cinema</option>
<option value="rivolaCinema">Rivola Cinema</option>
</select><br>
<label>Select day and time</label>
<select id="dayAndTime">
</select>
<br>
<label>Select Seat</label>
<select id="selectedSeat" onchange="getPrice()">
</select>
<p id="total">Total Price: </p>
</form>
</fieldset>
</body>
以及Javascipt部分:
function getPrice(){
var totalPrice = 0;
if(document.getElementById("selectedCinema").value == "maximaCinema"){
if(document.getElementById("dayAndTime").value == "9pmMonday" || document.getElementById("dayAndTime").value == "9pmTuesday"){
seatPrice["full"] = 12;
seatPrice["conc"] = 10;
seatPrice["child"] = 8;
seatPrice["FirstClass-Adult"] = 25;
seatPrice["FirstClass-Child"] = 20;
seatPrice["beanbag"] = 20;
}
else{
seatPrice["full"] = 18;
seatPrice["conc"] = 15;
seatPrice["child"] = 12;
seatPrice["FirstClass-Adult"] = 30;
seatPrice["FirstClass-Child"] = 25;
seatPrice["beanbag"] = 30;
}
}
else{
seatPrice["adult"] = 18;
seatPrice["conc"] = 15;
seatPrice["child"] = 12;
}
var seat = theForm.elements["selectedSeat"];
totalPrice = seatPrice[seat.value];
document.getElementById("total").innerHTML = "$" + totalPrice;
}
以下是JSFiddle中代码的完整版本:http://jsfiddle.net/stb0v5Ln/
谢谢。
- 始终使用jQuery,您的错误是使用form,而不是再次访问窗体或按ID访问窗体字段
- 将小提琴改为头,而不是加载
小提琴
var seat = $("#selectedSeat");
totalPrice = seatPrice[seat.val()];
$("#total").html("$" + totalPrice);
同时从标签中删除onchange并添加
$("#selectedSeat").change(getPrice);
到准备好的
ps:创建fiddle 时删除所有头部和身体标签
首先,fiddle被设置为运行onload
,因此它将把代码包装在一个onload处理程序中,因此函数getPrice()
在全局范围内不可用。
这将导致错误:
Uncaught ReferenceError: getPrice is not defined
将小提琴设置为无包装将解决此问题。
除此之外,JavaScript还有函数级作用域——您已经在ready()
函数中声明了变量var theForm = document.forms["ITSform"];
,并试图在getPrice()
函数中访问它。这将导致:
Uncaught ReferenceError: theForm is not defined
将变量设为全局变量或将函数getPrice()
移动到ready()
中
更新的Fiddle
只需在浏览器控制台中检查错误即可帮助您解决此类问题。
Js Fiddle
删除了未关闭正确的<fieldset>
并将CCD_ 9变量移动到CCD_
相关文章:
- Javascript onchange()函数不起作用
- Javascript变量未定义,onchange不起作用
- JavaScript onChange 事件侦听器对输入表单不起作用
- onchange Jquery 在使用选择器更改值时不起作用
- Javascript onchange(this.value,this.value)在ipad上不起作用
- 下拉列表的onChange()第二次不起作用
- Onchange组合框事件在中不起作用
- Onchange,向上,向下箭头键适用于FF和Chrome,但在IE中不起作用
- jQuery Datepicker onChange 事件不起作用
- 为什么从子窗口调用时父窗口的 OnChange 事件文本框不起作用
- onchange在页面加载时不起作用,直到我删除然后将其添加回代码
- HTML/Javascript onchange不起作用
- JavaScript,PHP,onchange选择框不起作用
- 为什么我的 onChange 事件不起作用
- 使用 React 触发 onchange 事件 - componentDidMount 不起作用
- Php select onchange dons'似乎不起作用
- 为什么不是'我的onchange函数不起作用
- Onchange函数使用laravel5不起作用
- onchange和Select不起作用
- JavaScript 显示/隐藏字段不起作用 - OnChange 不会每次都发生