为什么这个开关不能与我的 onchange 一起使用
我正在根据从下拉列表中选择的值更改变量。我有类似的使用 if/else 语句的方法,但是当我使用 switch 尝试它时,我无法让它工作。我在这里制作了 一个小提琴
html :
<select class="heiSelect">
<option value='3924'>6'1"</option>
<option value='3923'>6'2"</option>
<option value='3922'>6'3"</option>
<option value='3921'>6'4"</option>
<option value='3920'>6'5"</option>
<option value='3919'>6'6"</option>
<option value='3918'>6'7"</option>
<option value='3917'>6'8"</option>
<option value='3916'>6'9"</option>
<option value='3915'>6'10"</option>
</select>
<h1>Results : </h1>
javascript:
$(function() {
var hvd = 0;
$('.heiSelect').change(function() {
var heiValue = $('.heiSelect').val();
switch (heiValue) {
case 3915:
hvd = 4294964526;
break;
case 3916:
hvd = 4294964528;
break;
case 3917:
hvd = 4294964529;
break;
case 3918:
hvd = 4294964406;
break;
case 3919:
hvd = 4294964495;
break;
case 3920:
hvd = 4294964494;
break;
case 3921:
hvd = 4294964493;
break;
case 3922:
hvd = 4294964492;
break;
case 3923:
hvd = 4294964491;
break;
case 3924:
hvd = 4294964490;
break;
}
$('h1').append(heiValue + " + " + hvd + ", ");
});
});
你能看到问题吗?
I am changing a variable based on the value selected from a dropdown. I have something similar working with an if/else statement but when I try it with switch I can not get it to work. I made a fiddle of this here
html :
<select class="heiSelect">
<option value='3924'>6'1"</option>
<option value='3923'>6'2"</option>
<option value='3922'>6'3"</option>
<option value='3921'>6'4"</option>
<option value='3920'>6'5"</option>
<option value='3919'>6'6"</option>
<option value='3918'>6'7"</option>
<option value='3917'>6'8"</option>
<option value='3916'>6'9"</option>
<option value='3915'>6'10"</option>
</select>
<h1>Results : </h1>
javascript:
$(function() {
var hvd = 0;
$('.heiSelect').change(function() {
var heiValue = $('.heiSelect').val();
switch (heiValue) {
case 3915:
hvd = 4294964526;
break;
case 3916:
hvd = 4294964528;
break;
case 3917:
hvd = 4294964529;
break;
case 3918:
hvd = 4294964406;
break;
case 3919:
hvd = 4294964495;
break;
case 3920:
hvd = 4294964494;
break;
case 3921:
hvd = 4294964493;
break;
case 3922:
hvd = 4294964492;
break;
case 3923:
hvd = 4294964491;
break;
case 3924:
hvd = 4294964490;
break;
}
$('h1').append(heiValue + " + " + hvd + ", ");
});
});
Can you see the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您根据数字测试它们时,该值是一个字符串。
要修复此问题,请将字符串解析为整数:
或在以下情况下使用字符串:
The value is a string, while you tested them against numbers.
To fix it, either parse the string into integer:
or use strings in the cases:
heiValue
是一个字符串,您将其与switch
语句中的整数进行比较。将
heiValue
解析为整数,或者将 case 语句转换为字符串。heiValue
is a string, which you are comparing to integers in yourswitch
statement.Either parse
heiValue
into an integer, or convert your case statements to strings.