JQuery在更改时触发默认值。

huangapple go评论65阅读模式
英文:

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 () {
    $(&quot;#lang&quot;).on(&quot;change&quot;, function () {
        switch(this.value) {
            case &quot;pl&quot;:
                $(&quot;#content&quot;).load(&quot;pl.html&quot;);
            case &quot;en&quot;:
                $(&quot;#content&quot;).load(&quot;en.html&quot;);
            default:
                alert(&quot;default&quot;)
        }
    });
});
&lt;select name=&quot;lang&quot; id=&quot;lang&quot;&gt;
    &lt;option value=&quot;en&quot;&gt;en&lt;/option&gt;
    &lt;option value=&quot;pl&quot;&gt;pl&lt;/option&gt;
&lt;/select&gt;
&lt;div id=&quot;content&quot;&gt;&lt;/div&gt;

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.

enter link description here

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  $(&quot;#lang&quot;).on(&quot;change&quot;, function() {
    switch (this.value) {
      case &quot;pl&quot;:
        $(&quot;#content&quot;).load(&quot;pl.html&quot;);
        break;
      case &quot;en&quot;:
        $(&quot;#content&quot;).load(&quot;en.html&quot;);
        break;
      default:
        alert(&quot;default&quot;)
        break;
    }
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;select name=&quot;lang&quot; id=&quot;lang&quot;&gt;
  &lt;option value=&quot;en&quot;&gt;en&lt;/option&gt;
  &lt;option value=&quot;pl&quot;&gt;pl&lt;/option&gt;
&lt;/select&gt;
&lt;div id=&quot;content&quot;&gt;&lt;/div&gt;

<!-- 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.

enter link description here

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  $(&quot;#lang&quot;).on(&quot;change&quot;, function() {
    switch (this.value) {
      case &quot;pl&quot;:
        $(&quot;#content&quot;).load(&quot;pl.html&quot;);
        break;
      case &quot;en&quot;:
        $(&quot;#content&quot;).load(&quot;en.html&quot;);
        break;
      default:
        alert(&quot;default&quot;)
        break;
    }
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;select name=&quot;lang&quot; id=&quot;lang&quot;&gt;
  &lt;option value=&quot;en&quot;&gt;en&lt;/option&gt;
  &lt;option value=&quot;pl&quot;&gt;pl&lt;/option&gt;
&lt;/select&gt;
&lt;div id=&quot;content&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月24日 17:43:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定