首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过选择值改变数值的最小值/最大值?

如何通过选择值改变数值的最小值/最大值?
EN

Stack Overflow用户
提问于 2020-07-30 22:19:11
回答 1查看 262关注 0票数 1

我有两个值select和number

代码语言:javascript
复制
<select id="select-1" name="select-1" >
<option value="product-1">Product 1</option>
<option value="product-2">Product 2</option>
</select>
<input type="number" name="number-1"  id="field-number-1" pattern="^\-?\d*([\.\,]\d+)?" inputmode="numeric" min="100" max="500" />

使用jQuery尝试通过选择不同的产品进行最小/最大值更新,但不进行数值更新

代码语言:javascript
复制
 <script type='text/javascript'>
    jQuery(document).ready(function(){
        jQuery('#select-1').on('change', function() {
          if ( this.value == 'product-1') {    
            jQuery('#field-number-1').attr('min', '100');
            jQuery('#field-number-1').attr('max', '200');              
          } else if(this.value == 'product-2') {
            jQuery('#field-number-1').attr('min', '300');
            jQuery('#field-number-1').attr('max', '500');              
          } else {    
            jQuery('#field-number-1').attr('min', '100');
            jQuery('#field-number-1').attr('max', '500');
          
          }
        });
    });
  </script>
EN

回答 1

Stack Overflow用户

发布于 2020-07-30 22:42:44

您当前的代码应该可以很好地工作,但是,因为您的表单在默认情况下选择了product-1,所以您需要确保使用.change()在DOM ready上触发更改事件。这样,默认选择与maxmin值将保持一致。

代码语言:javascript
复制
jQuery(document).ready(function(){
        jQuery('#select-1').on('change', function() {
          if ( this.value == 'product-1') {    
            jQuery('#field-number-1').attr('min', '100');
            jQuery('#field-number-1').attr('max', '200');              
          } else if(this.value == 'product-2') {
            jQuery('#field-number-1').attr('min', '300');
            jQuery('#field-number-1').attr('max', '500');              
          } else {    
            jQuery('#field-number-1').attr('min', '100');
            jQuery('#field-number-1').attr('max', '500');
          }
          //Report if current value is valid
          jQuery('#field-number-1')[0].reportValidity();
          //delete this .... just for testing
          console.log( jQuery('#field-number-1')[0] );
        })
        .change();
        
        jQuery('#field-number-1').on('input', function(e) {
            console.log( this.checkValidity() );
            if( !this.checkValidity() ) {
                this.setCustomValidity(`Provide a number between ${$(this).attr('min')} and ${$(this).attr('max')}`);
                this.reportValidity();
            } else {
                this.setCustomValidity('');
                this.reportValidity();
            }
        });
    });
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="select-1" name="select-1" >
<option value="product-1">Product 1</option>
<option value="product-2">Product 2</option>
</select>
<input type="number" name="number-1"  id="field-number-1" pattern="^\-?\d*([\.\,]\d+)?" inputmode="numeric" min="100" max="500" />

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63175166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档