• <legend id='nOS6P'><style id='nOS6P'><dir id='nOS6P'><q id='nOS6P'></q></dir></style></legend>

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

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

      2. <tfoot id='nOS6P'></tfoot>
          <bdo id='nOS6P'></bdo><ul id='nOS6P'></ul>

        如何使用 JavaScript 限制最小值/最大值之间的数字

        时间:2023-09-07

          • <bdo id='9uzCk'></bdo><ul id='9uzCk'></ul>

            <legend id='9uzCk'><style id='9uzCk'><dir id='9uzCk'><q id='9uzCk'></q></dir></style></legend>

            <tfoot id='9uzCk'></tfoot>
          • <small id='9uzCk'></small><noframes id='9uzCk'>

            <i id='9uzCk'><tr id='9uzCk'><dt id='9uzCk'><q id='9uzCk'><span id='9uzCk'><b id='9uzCk'><form id='9uzCk'><ins id='9uzCk'></ins><ul id='9uzCk'></ul><sub id='9uzCk'></sub></form><legend id='9uzCk'></legend><bdo id='9uzCk'><pre id='9uzCk'><center id='9uzCk'></center></pre></bdo></b><th id='9uzCk'></th></span></q></dt></tr></i><div id='9uzCk'><tfoot id='9uzCk'></tfoot><dl id='9uzCk'><fieldset id='9uzCk'></fieldset></dl></div>
                    <tbody id='9uzCk'></tbody>
                  本文介绍了如何使用 JavaScript 限制最小值/最大值之间的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想限制两个值之间的数字,我知道在 PHP 中你可以这样做:

                  I want to limit a number between two values, I know that in PHP you can do this:

                  $number = min(max(intval($number), 1), 20);
                  // this will make $number 1 if it's lower than 1, and 20 if it's higher than 20
                  

                  我怎样才能在 javascript 中做到这一点,而不必编写多个 if 语句和类似的东西?谢谢.

                  How can I do this in javascript, without having to write multiple if statements and stuff like that? Thanks.

                  推荐答案

                  这样的

                  var number = Math.min(Math.max(parseInt(number), 1), 20);
                  

                  现场演示:

                  function limitNumberWithinRange(num, min, max){
                    const MIN = min || 1;
                    const MAX = max || 20;
                    const parsed = parseInt(num)
                    return Math.min(Math.max(parsed, MIN), MAX)
                  }
                  
                  alert(
                    limitNumberWithinRange(  prompt("enter a number")   )
                  )

                  这篇关于如何使用 JavaScript 限制最小值/最大值之间的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在javascript中使用按位或转换为整数 下一篇:输入模型在更改时从 Integer 更改为 String

                  相关文章

                  最新文章

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

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

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

                      <legend id='p8QWk'><style id='p8QWk'><dir id='p8QWk'><q id='p8QWk'></q></dir></style></legend>