英文:
JQuery on change hitting default
问题
我正在尝试使用选择框动态加载内容。
$(document).ready(function () {
$("#lang").on("change", function () {
switch(this.value) {
case "pl":
$("#content").load("pl.html");
break;
case "en":
$("#content").load("en.html");
break;
default:
alert("default");
}
});
});
<select name="lang" id="lang">
<option value="en">en</option>
<option value="pl">pl</option>
</select>
<div id="content"></div>
每当我更改值时,默认警报都会出现,并且内容不总是正确更新。
英文:
I am trying to dynamically load content with select.
$(document).ready(function () {
$("#lang").on("change", function () {
switch(this.value) {
case "pl":
$("#content").load("pl.html");
case "en":
$("#content").load("en.html");
default:
alert("default")
}
});
});
<select name="lang" id="lang">
<option value="en">en</option>
<option value="pl">pl</option>
</select>
<div id="content"></div>
Whenever I am changing value the default alert is occurring and the content is not always updated properly.
答案1
得分: 1
If break is omitted, execution will proceed to the next case clause, even to the default clause, regardless of whether the value of that clause matches.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$("#lang").on("change", function() {
switch (this.value) {
case "pl":
$("#content").load("pl.html");
break;
case "en":
$("#content").load("en.html");
break;
default:
alert("default")
break;
}
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="lang" id="lang">
<option value="en">en</option>
<option value="pl">pl</option>
</select>
<div id="content"></div>
<!-- end snippet -->
英文:
If break is omitted, execution will proceed to the next case clause, even to the default clause, regardless of whether the value of that clause matches.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$("#lang").on("change", function() {
switch (this.value) {
case "pl":
$("#content").load("pl.html");
break;
case "en":
$("#content").load("en.html");
break;
default:
alert("default")
break;
}
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="lang" id="lang">
<option value="en">en</option>
<option value="pl">pl</option>
</select>
<div id="content"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论