Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript html数字输入框根据以下单位标签进行更改_Javascript_Html - Fatal编程技术网

Javascript html数字输入框根据以下单位标签进行更改

Javascript html数字输入框根据以下单位标签进行更改,javascript,html,Javascript,Html,我有一个HTML数字输入,并遵循单位类型的选择 M 嗯 英寸 var foo=document.getElementByIdfoo 函数更改本身{ ifNumberself.valueNumberself.maxself.value=self.max; } 函数changeUnitStep{ foo.value=foo.value*unitStep; foo.min=foo.min*unitStep; foo.max=foo.max*unitStep; } M 嗯 v

我有一个HTML数字输入,并遵循单位类型的选择

M 嗯 英寸 var foo=document.getElementByIdfoo 函数更改本身{ ifNumberself.valueNumberself.maxself.value=self.max; } 函数changeUnitStep{ foo.value=foo.value*unitStep; foo.min=foo.min*unitStep; foo.max=foo.max*unitStep; } M 嗯 var foo=document.getElementByIdfoo 函数更改本身{ ifNumberself.valueNumberself.maxself.value=self.max; } 函数changeUnitStep{ foo.value=foo.value*unitStep; foo.min=foo.min*unitStep; foo.max=foo.max*unitStep; } M 嗯
看起来很接近,但您需要对更改的选择选项做出反应,而不是更改输入字段

更改选择选项后,只需根据所选选项设置最小/最大值,然后根据需要转换单位

我还将此逻辑移到了一个名为update的函数中,而不是尝试将其内联-如果它不仅仅是一两条简单的语句,那么很难读取和调试它

考虑以下工作片段:

函数更新lem{ 常量输入=document.getElementById'foo'; 如果元素值=='1000'{ 设置属性'min','1'; 设置属性'max','10'; input.value/=1000; }否则{ 设置属性'min','1000'; setAttribute'max','10000'; 输入值*=1000; } } M 嗯
看起来很接近,但您需要对更改的选择选项做出反应,而不是更改输入字段

更改选择选项后,只需根据所选选项设置最小/最大值,然后根据需要转换单位

我还将此逻辑移到了一个名为update的函数中,而不是尝试将其内联-如果它不仅仅是一两条简单的语句,那么很难读取和调试它

考虑以下工作片段:

函数更新lem{ 常量输入=document.getElementById'foo'; 如果元素值=='1000'{ 设置属性'min','1'; 设置属性'max','10'; input.value/=1000; }否则{ 设置属性'min','1000'; setAttribute'max','10000'; 输入值*=1000; } } M 嗯
只需在标记中添加一个onchange事件:

功能转换单元{ var x=document.getElementByIdfoo-unit.value; 如果x==1{ document.getElementByIdfoo.max=10000; document.getElementByIdfoo.min=1000; } } M 嗯
只需在标记中添加一个onchange事件:

功能转换单元{ var x=document.getElementByIdfoo-unit.value; 如果x==1{ document.getElementByIdfoo.max=10000; document.getElementByIdfoo.min=1000; } } M 嗯 const option=document.getElementById'foo-unit'; const inputEl=document.getElementById'foo'; option.addEventListener'change',函数{ ifoption.value==='mm'{ var inputValue=inputEl.value; inputEl.value=inputValue*1000; setAttribute'min','1000'; setAttribute'max','10000'; }否则{ var inputValue=inputEl.value; inputEl.value=inputValue/1000; setAttribute'min','1'; setAttribute'max','10'; } } M 嗯 const option=document.getElementById'foo-unit'; const inputEl=document.getElementById'foo'; option.addEventListener'change',函数{ ifoption.value==='mm'{ var inputValue=inputEl.value; inputEl.value=inputValue*1000; setAttribute'min','1000'; setAttribute'max','10000'; }否则{ var inputValue=inputEl.value; inputEl.value=inputValue/1000; setAttribute'min','1'; setAttribute'max','10'; } } M 嗯
尝试下面的代码,您可以添加选项来选择新的单位

功能转换单元{ var input=document.getElementByIdinput; var unitSelect=document.getElementByIdunitSelect; var currentConvert=Number.parseFloatdocument.querySelectoroption[value=+input.getAttributeunit+]getAttributeconvert; var newUnit=document.queryselector选项[value=+unitSelect.value+]; var newConvert=Number.parseFloatnewUnit.getAttributeconvert; var currentValue=Number.parseFloatinput.value; //转换为新单位 var newValue=currentValue*currentConvert/newConvert; //将新值、单位、最小值和最大值设置为输入 input.value=Number.parseFloatnewValue; input.setAttribute'unit',newUnit.value; input.setAttribute'min',newUnit.getAttribute'min'; input.setAttribute'max',newUnit.getAttribute'max'; } M 厘米 嗯 英寸
尝试下面的代码,您可以添加选项来选择新的单位

功能转换单元{ var input=document.getElementByIdinput; var unitSelect=document.getElementByIdunitSelect; var currentConvert=Number.parseFloatdocument.querySelectoroption[value=+input.getAttributeunit+]getAttributeconvert; var newUnit=document.queryselector选项[value=+unitSelect.value+]; var newConvert=Number.parseFloatnewUnit.getAttributeconvert; var currentValue=Number.parseFloatinput.value; //转换为新单位 var newValue=currentValue*currentConvert/newConvert; //将新值、单位、最小值和最大值设置为输入 input.value=Number.parseFloatnewValue; input.setAttribute'unit',newUnit.value; input.setAttribute'min',newUnit.getAttribute'min'; input.setAttribute'max',newUnit.getAttribute'max'; } M 厘米 嗯 英寸
如果有两个以上的选项,比如米、毫米和英寸,该怎么办?在这种情况下,值是如何转换的?您可以很容易地将其概括为更改各个单位的最小值/最大值。但是,要进行转换,您需要在更改当前单位之前对其进行说明。如果有两个以上的选项,例如米、毫米和英寸,该怎么办?在这种情况下,值是如何转换的?您可以很容易地将其概括为更改各个单位的最小值/最大值。但是,要进行转换,您需要在更改当前单位之前对其进行说明