• <tfoot id='NDx7n'></tfoot>
      <i id='NDx7n'><tr id='NDx7n'><dt id='NDx7n'><q id='NDx7n'><span id='NDx7n'><b id='NDx7n'><form id='NDx7n'><ins id='NDx7n'></ins><ul id='NDx7n'></ul><sub id='NDx7n'></sub></form><legend id='NDx7n'></legend><bdo id='NDx7n'><pre id='NDx7n'><center id='NDx7n'></center></pre></bdo></b><th id='NDx7n'></th></span></q></dt></tr></i><div id='NDx7n'><tfoot id='NDx7n'></tfoot><dl id='NDx7n'><fieldset id='NDx7n'></fieldset></dl></div>

      <small id='NDx7n'></small><noframes id='NDx7n'>

          <bdo id='NDx7n'></bdo><ul id='NDx7n'></ul>

      1. <legend id='NDx7n'><style id='NDx7n'><dir id='NDx7n'><q id='NDx7n'></q></dir></style></legend>

        JavaScript - 一种检查 &lt;select&gt; 选项的方法标签被选中

        时间:2023-07-31
            <tbody id='IlnYG'></tbody>
              <legend id='IlnYG'><style id='IlnYG'><dir id='IlnYG'><q id='IlnYG'></q></dir></style></legend>

                <bdo id='IlnYG'></bdo><ul id='IlnYG'></ul>

                <small id='IlnYG'></small><noframes id='IlnYG'>

                <tfoot id='IlnYG'></tfoot>

                  <i id='IlnYG'><tr id='IlnYG'><dt id='IlnYG'><q id='IlnYG'><span id='IlnYG'><b id='IlnYG'><form id='IlnYG'><ins id='IlnYG'></ins><ul id='IlnYG'></ul><sub id='IlnYG'></sub></form><legend id='IlnYG'></legend><bdo id='IlnYG'><pre id='IlnYG'><center id='IlnYG'></center></pre></bdo></b><th id='IlnYG'></th></span></q></dt></tr></i><div id='IlnYG'><tfoot id='IlnYG'></tfoot><dl id='IlnYG'><fieldset id='IlnYG'></fieldset></dl></div>
                  本文介绍了JavaScript - 一种检查 &lt;select&gt; 选项的方法标签被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个选择标签,我想检查是否选择了飞蛾.

                  I have a select tag and I want to check if a moth is selected.

                   <select name="Birth_Month">
                      <option value="" SELECTED>- Month -</option>
                      <option value="January">January</option>
                      <option value="Fabruary">Fabruary</option>
                      <option value="March">March</option>
                      <option value="April">April</option>
                      <option value="May">May</option>
                      <option value="June">June</option>
                      <option value="July">July</option>
                      <option value="August">August</option>
                      <option value="September">September</option>
                      <option value="October">October</option>
                      <option value="November">November</option>
                      <option value="December">December</option>
                  </select>
                  

                  这样做:

                  if (document.registration_form.Birth_Month.value === '') 
                  {
                      alert('Please fill select Month!');
                  }
                  

                  但是这个 JavaScript 对于某些 select-s 有效,而对于其中一些则无效.Obviously, when the "- Month -" is selected the it returnes "- Month -" insted of "" (empty string).我做错了什么?检查标签选择的最佳方法是什么?

                  But this JavaScript for some select-s work and for some of them, does not. Obviously, when the "- Month -" is selected the it returnes "- Month -" insted of "" (empty string). What I have done wrong? What is the best way of checking the tag's selection?

                  推荐答案

                  浏览器并不总是有一个用于 <select> 的 .value 属性.- 我们以前必须得到<option>的值:

                  Browsers didn't always have a .value property for <select> - we used to have to get the value of the <option>:

                  var birthMonth = document.registration_form.Birth_Month;
                  if (birthMonth.options[birthMonth.selectedIndex].value === '') {
                    // something
                  }
                  

                  我现在使用 jQuery .val().我不记得哪些浏览器缺少 select.value 属性,也许这些浏览器太旧了,我们不需要再担心它们了.但是 jQuery 不使用 select.value - 它遍历每个选项以查找所选选项的值.

                  I use jQuery .val() now. I don't remember which browsers lack the select.value property, and maybe those browsers are so old that we don't need to worry about them anymore. But jQuery doesn't use select.value - it loops through each of the options to find the selected option's value.

                  当然,如果您知道您将始终将一个空白选项作为第一个选项,只需检查 selectedIndex==0.

                  Of course, if you know you'll always have a single blank option as the first option, just check for selectedIndex==0.

                  这篇关于JavaScript - 一种检查 &lt;select&gt; 选项的方法标签被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:“a"是什么意思?在 html &lt;a&gt;标签代表什么? 下一篇:设置包含与号 (&amp;) 的主题的 mailto 链接

                  相关文章

                1. <legend id='qYwLa'><style id='qYwLa'><dir id='qYwLa'><q id='qYwLa'></q></dir></style></legend>
                2. <small id='qYwLa'></small><noframes id='qYwLa'>

                  <tfoot id='qYwLa'></tfoot>

                    1. <i id='qYwLa'><tr id='qYwLa'><dt id='qYwLa'><q id='qYwLa'><span id='qYwLa'><b id='qYwLa'><form id='qYwLa'><ins id='qYwLa'></ins><ul id='qYwLa'></ul><sub id='qYwLa'></sub></form><legend id='qYwLa'></legend><bdo id='qYwLa'><pre id='qYwLa'><center id='qYwLa'></center></pre></bdo></b><th id='qYwLa'></th></span></q></dt></tr></i><div id='qYwLa'><tfoot id='qYwLa'></tfoot><dl id='qYwLa'><fieldset id='qYwLa'></fieldset></dl></div>

                      • <bdo id='qYwLa'></bdo><ul id='qYwLa'></ul>