JS中如何限制位数从6到4? (输入字段类型编号和只读)
How do I limit the number of digits from 6 to 4 in JS? (imput field type number and readonly)
我正在尝试为我的网站构建一个卡路里计算器。字段给我这样的结果 0.000.000。我正在尝试解决问题并仅显示 4 位数字,如下所示:0.000
我不知道这个功能是什么,我是一个粉丝,没有编程技能。请帮忙!
https://jsfiddle.net/snake93/89wtnxj4/273/
<!--Input Field-->
<label class="mts-label">Peso</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="3" id="weight" name"weight1" placeholder="es: 70Kg"/>
<label class="mts-label">Altezza</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="3" id="height" name"height1" placeholder="es: 170cm"/>
<label class="mts-label">Età</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="2" id="age" name"age1" placeholder="es: 25 anni"/>
<label class="mts-label">Bodyfat in %</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="2" id="bodyfat" name"bodyfat1" placeholder="es: 15%"/>
<div class="mts-label">Sesso</div>
<!--Radio Button Sex-->
<div class="mts-radio-button">
<input type="radio" id="sexuomo" name="radios" value="Male">
<label class="mts-label-radio" for="sexuomo">Uomo</label>
</div>
<div class="mts-radio-button1">
<input type="radio" id="sexdonna" name="radios" value="Female">
<label class="mts-label-radio" for="sexdonna">Donna</label>
</div>
<!--Radio Activity Level-->
<br>
<label class="mts-label">Attività lavorativa</label>
<div class="mts-radio-button">
<input type="radio" id="leggero" name="radiosa" value="1.2" onclick="calculate()">
<label class="mts-label-radio" for="leggero">Leggero</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="attivo" name="radiosa" value="1.375" onclick="calculate()">
<label class="mts-label-radio" for="attivo">Attivo</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="allenato" name="radiosa" value="1.55" onclick="calculate()">
<label class="mts-label-radio" for="allenato">Allenato</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="Mattivo" name="radiosa" value="1.75" onclick="calculate()">
<label class="mts-label-radio" for="Mattivo">Molto Attivo</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="Eattivo" name="radiosa" value="1.9" onclick="calculate()">
<label class="mts-label-radio" for="Eattivo">Estremamente Attivo</label>
</div>
<!---BMR Mifflin StJeor Result Field--->
<br>
<label class="mts-label">BMR Mifflin St Jeor Formula</label><br>
<div id="bmr-sexuomo" hidden>
<label class="mts-label">Male</label><br>
<input type="text" class="mts-field" id="bmr_mifflin_man" name="bmr_mifflin_man"
placeholder="0.000,0 Kcal" min="1" readonly/>
</div>
<br>
<div id="bmr-sexdonna" hidden>
<label class="mts-label">Female</label><br>
<input type="text" class="mts-field" id="bmr_mifflin_woman" name="bmr_mifflin_woman"
placeholder="0.000,0 Kcal" min="1" readonly/>
</div>
<!---TDEE Mifflin StJeor Result Field--->
<br>
<label class="mts-label">TDEE Mifflin St Jeor Formula</label><br>
<div id="MifflinMale" hidden>
<label class="mts-label">Male</label><br>
<input type="text" class="mts-field" id="tdee_mifflin_man" name="tdee_mifflin_man"
placeholder="0.000,0 Kcal Uomo" min="1" readonly/>
</div>
<br>
<div id="MifflinFemale" hidden>
<label class="mts-label">Female</label><br>
<input type="text" class="mts-field" id="tdee_mifflin_woman" name="tdee_mifflin_woman"
placeholder="0.000,0 Kcal donna" min="1" readonly/>
</div>
<!---BMR Katch McArdle Formula--->
<br>
<label class="mts-label">BMR Katch McArdle Formula</label>
<div id="Ktch">
<input type="text" class="mts-field" id="bmr_katch" name="bmr_katch"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---TDEE Katch McArdle Formula--->
<br>
<label class="mts-label">TDEE Katch McArdle Formula</label>
<div id="Ktch">
<input type="text" class="mts-field" id="tdee_katch" name="tdee_katch"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---BMR Cunningham Formula--->
<br>
<label class="mts-label">BMR Cunningham Formula</label>
<div id="Cunningham">
<input type="text" class="mts-field" id="bmr_cunningham" name="bmr_cunningham"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---TDEE Cunningham Formula--->
<br>
<label class="mts-label">TDEE Cunningham Formula</label>
<div id="Cunningham">
<input type="text" class="mts-field" id="tdee_cunningham" name="tdee_cunningham"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---Calc & Reset Button--->
<br>
<button name="calculate" onclick="calculate()">Re-Calculate</button>
<button id="reset" onclick="resetFields()">Reset</button>
<!--Input Field-->
<label class="mts-label">Peso</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="3" id="weight" name"weight1" placeholder="es: 70Kg"/>
<label class="mts-label">Altezza</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="3" id="height" name"height1" placeholder="es: 170cm"/>
<label class="mts-label">Età</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="2" id="age" name"age1" placeholder="es: 25 anni"/>
<label class="mts-label">Bodyfat in %</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="2" id="bodyfat" name"bodyfat1" placeholder="es: 15%"/>
<div class="mts-label">Sesso</div>
<!--Radio Button Sex-->
<div class="mts-radio-button">
<input type="radio" id="sexuomo" name="radios" value="Male">
<label class="mts-label-radio" for="sexuomo">Uomo</label>
</div>
<div class="mts-radio-button1">
<input type="radio" id="sexdonna" name="radios" value="Female">
<label class="mts-label-radio" for="sexdonna">Donna</label>
</div>
<!--Radio Activity Level-->
<br>
<label class="mts-label">Attività lavorativa</label>
<div class="mts-radio-button">
<input type="radio" id="leggero" name="radiosa" value="1.2" onclick="calculate()">
<label class="mts-label-radio" for="leggero">Leggero</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="attivo" name="radiosa" value="1.375" onclick="calculate()">
<label class="mts-label-radio" for="attivo">Attivo</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="allenato" name="radiosa" value="1.55" onclick="calculate()">
<label class="mts-label-radio" for="allenato">Allenato</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="Mattivo" name="radiosa" value="1.75" onclick="calculate()">
<label class="mts-label-radio" for="Mattivo">Molto Attivo</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="Eattivo" name="radiosa" value="1.9" onclick="calculate()">
<label class="mts-label-radio" for="Eattivo">Estremamente Attivo</label>
</div>
<!---BMR Mifflin StJeor Result Field--->
<br>
<label class="mts-label">BMR Mifflin St Jeor Formula</label><br>
<div id="bmr-sexuomo" hidden>
<label class="mts-label">Male</label><br>
<input type="text" class="mts-field" id="bmr_mifflin_man" name="bmr_mifflin_man"
placeholder="0.000,0 Kcal" min="1" readonly/>
</div>
<br>
<div id="bmr-sexdonna" hidden>
<label class="mts-label">Female</label><br>
<input type="text" class="mts-field" id="bmr_mifflin_woman" name="bmr_mifflin_woman"
placeholder="0.000,0 Kcal" min="1" readonly/>
</div>
<!---TDEE Mifflin StJeor Result Field--->
<br>
<label class="mts-label">TDEE Mifflin St Jeor Formula</label><br>
<div id="MifflinMale" hidden>
<label class="mts-label">Male</label><br>
<input type="text" class="mts-field" id="tdee_mifflin_man" name="tdee_mifflin_man"
placeholder="0.000,0 Kcal Uomo" min="1" readonly/>
</div>
<br>
<div id="MifflinFemale" hidden>
<label class="mts-label">Female</label><br>
<input type="text" class="mts-field" id="tdee_mifflin_woman" name="tdee_mifflin_woman"
placeholder="0.000,0 Kcal donna" min="1" readonly/>
</div>
<!---BMR Katch McArdle Formula--->
<br>
<label class="mts-label">BMR Katch McArdle Formula</label>
<div id="Ktch">
<input type="text" class="mts-field" id="bmr_katch" name="bmr_katch"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---TDEE Katch McArdle Formula--->
<br>
<label class="mts-label">TDEE Katch McArdle Formula</label>
<div id="Ktch">
<input type="text" class="mts-field" id="tdee_katch" name="tdee_katch"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---BMR Cunningham Formula--->
<br>
<label class="mts-label">BMR Cunningham Formula</label>
<div id="Cunningham">
<input type="text" class="mts-field" id="bmr_cunningham" name="bmr_cunningham"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---TDEE Cunningham Formula--->
<br>
<label class="mts-label">TDEE Cunningham Formula</label>
<div id="Cunningham">
<input type="text" class="mts-field" id="tdee_cunningham" name="tdee_cunningham"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---Calc & Reset Button--->
<br>
<button name="calculate" onclick="calculate()">Re-Calculate</button>
<button id="reset" onclick="resetFields()">Reset</button>
您需要告诉 toLocaleString
您不需要数字:
document.getElementById('tdee_cunningham').value = bmr_cunningham.toLocaleString('it-IT', { maximumFractionDigits: 0 } + " Kcal";
我正在尝试为我的网站构建一个卡路里计算器。字段给我这样的结果 0.000.000。我正在尝试解决问题并仅显示 4 位数字,如下所示:0.000
我不知道这个功能是什么,我是一个粉丝,没有编程技能。请帮忙!
https://jsfiddle.net/snake93/89wtnxj4/273/
<!--Input Field-->
<label class="mts-label">Peso</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="3" id="weight" name"weight1" placeholder="es: 70Kg"/>
<label class="mts-label">Altezza</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="3" id="height" name"height1" placeholder="es: 170cm"/>
<label class="mts-label">Età</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="2" id="age" name"age1" placeholder="es: 25 anni"/>
<label class="mts-label">Bodyfat in %</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="2" id="bodyfat" name"bodyfat1" placeholder="es: 15%"/>
<div class="mts-label">Sesso</div>
<!--Radio Button Sex-->
<div class="mts-radio-button">
<input type="radio" id="sexuomo" name="radios" value="Male">
<label class="mts-label-radio" for="sexuomo">Uomo</label>
</div>
<div class="mts-radio-button1">
<input type="radio" id="sexdonna" name="radios" value="Female">
<label class="mts-label-radio" for="sexdonna">Donna</label>
</div>
<!--Radio Activity Level-->
<br>
<label class="mts-label">Attività lavorativa</label>
<div class="mts-radio-button">
<input type="radio" id="leggero" name="radiosa" value="1.2" onclick="calculate()">
<label class="mts-label-radio" for="leggero">Leggero</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="attivo" name="radiosa" value="1.375" onclick="calculate()">
<label class="mts-label-radio" for="attivo">Attivo</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="allenato" name="radiosa" value="1.55" onclick="calculate()">
<label class="mts-label-radio" for="allenato">Allenato</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="Mattivo" name="radiosa" value="1.75" onclick="calculate()">
<label class="mts-label-radio" for="Mattivo">Molto Attivo</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="Eattivo" name="radiosa" value="1.9" onclick="calculate()">
<label class="mts-label-radio" for="Eattivo">Estremamente Attivo</label>
</div>
<!---BMR Mifflin StJeor Result Field--->
<br>
<label class="mts-label">BMR Mifflin St Jeor Formula</label><br>
<div id="bmr-sexuomo" hidden>
<label class="mts-label">Male</label><br>
<input type="text" class="mts-field" id="bmr_mifflin_man" name="bmr_mifflin_man"
placeholder="0.000,0 Kcal" min="1" readonly/>
</div>
<br>
<div id="bmr-sexdonna" hidden>
<label class="mts-label">Female</label><br>
<input type="text" class="mts-field" id="bmr_mifflin_woman" name="bmr_mifflin_woman"
placeholder="0.000,0 Kcal" min="1" readonly/>
</div>
<!---TDEE Mifflin StJeor Result Field--->
<br>
<label class="mts-label">TDEE Mifflin St Jeor Formula</label><br>
<div id="MifflinMale" hidden>
<label class="mts-label">Male</label><br>
<input type="text" class="mts-field" id="tdee_mifflin_man" name="tdee_mifflin_man"
placeholder="0.000,0 Kcal Uomo" min="1" readonly/>
</div>
<br>
<div id="MifflinFemale" hidden>
<label class="mts-label">Female</label><br>
<input type="text" class="mts-field" id="tdee_mifflin_woman" name="tdee_mifflin_woman"
placeholder="0.000,0 Kcal donna" min="1" readonly/>
</div>
<!---BMR Katch McArdle Formula--->
<br>
<label class="mts-label">BMR Katch McArdle Formula</label>
<div id="Ktch">
<input type="text" class="mts-field" id="bmr_katch" name="bmr_katch"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---TDEE Katch McArdle Formula--->
<br>
<label class="mts-label">TDEE Katch McArdle Formula</label>
<div id="Ktch">
<input type="text" class="mts-field" id="tdee_katch" name="tdee_katch"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---BMR Cunningham Formula--->
<br>
<label class="mts-label">BMR Cunningham Formula</label>
<div id="Cunningham">
<input type="text" class="mts-field" id="bmr_cunningham" name="bmr_cunningham"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---TDEE Cunningham Formula--->
<br>
<label class="mts-label">TDEE Cunningham Formula</label>
<div id="Cunningham">
<input type="text" class="mts-field" id="tdee_cunningham" name="tdee_cunningham"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---Calc & Reset Button--->
<br>
<button name="calculate" onclick="calculate()">Re-Calculate</button>
<button id="reset" onclick="resetFields()">Reset</button>
<!--Input Field-->
<label class="mts-label">Peso</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="3" id="weight" name"weight1" placeholder="es: 70Kg"/>
<label class="mts-label">Altezza</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="3" id="height" name"height1" placeholder="es: 170cm"/>
<label class="mts-label">Età</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="2" id="age" name"age1" placeholder="es: 25 anni"/>
<label class="mts-label">Bodyfat in %</label>
<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" type="number" class="mts-field" maxlength="2" id="bodyfat" name"bodyfat1" placeholder="es: 15%"/>
<div class="mts-label">Sesso</div>
<!--Radio Button Sex-->
<div class="mts-radio-button">
<input type="radio" id="sexuomo" name="radios" value="Male">
<label class="mts-label-radio" for="sexuomo">Uomo</label>
</div>
<div class="mts-radio-button1">
<input type="radio" id="sexdonna" name="radios" value="Female">
<label class="mts-label-radio" for="sexdonna">Donna</label>
</div>
<!--Radio Activity Level-->
<br>
<label class="mts-label">Attività lavorativa</label>
<div class="mts-radio-button">
<input type="radio" id="leggero" name="radiosa" value="1.2" onclick="calculate()">
<label class="mts-label-radio" for="leggero">Leggero</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="attivo" name="radiosa" value="1.375" onclick="calculate()">
<label class="mts-label-radio" for="attivo">Attivo</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="allenato" name="radiosa" value="1.55" onclick="calculate()">
<label class="mts-label-radio" for="allenato">Allenato</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="Mattivo" name="radiosa" value="1.75" onclick="calculate()">
<label class="mts-label-radio" for="Mattivo">Molto Attivo</label>
</div>
<div class="mts-radio-button">
<input type="radio" id="Eattivo" name="radiosa" value="1.9" onclick="calculate()">
<label class="mts-label-radio" for="Eattivo">Estremamente Attivo</label>
</div>
<!---BMR Mifflin StJeor Result Field--->
<br>
<label class="mts-label">BMR Mifflin St Jeor Formula</label><br>
<div id="bmr-sexuomo" hidden>
<label class="mts-label">Male</label><br>
<input type="text" class="mts-field" id="bmr_mifflin_man" name="bmr_mifflin_man"
placeholder="0.000,0 Kcal" min="1" readonly/>
</div>
<br>
<div id="bmr-sexdonna" hidden>
<label class="mts-label">Female</label><br>
<input type="text" class="mts-field" id="bmr_mifflin_woman" name="bmr_mifflin_woman"
placeholder="0.000,0 Kcal" min="1" readonly/>
</div>
<!---TDEE Mifflin StJeor Result Field--->
<br>
<label class="mts-label">TDEE Mifflin St Jeor Formula</label><br>
<div id="MifflinMale" hidden>
<label class="mts-label">Male</label><br>
<input type="text" class="mts-field" id="tdee_mifflin_man" name="tdee_mifflin_man"
placeholder="0.000,0 Kcal Uomo" min="1" readonly/>
</div>
<br>
<div id="MifflinFemale" hidden>
<label class="mts-label">Female</label><br>
<input type="text" class="mts-field" id="tdee_mifflin_woman" name="tdee_mifflin_woman"
placeholder="0.000,0 Kcal donna" min="1" readonly/>
</div>
<!---BMR Katch McArdle Formula--->
<br>
<label class="mts-label">BMR Katch McArdle Formula</label>
<div id="Ktch">
<input type="text" class="mts-field" id="bmr_katch" name="bmr_katch"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---TDEE Katch McArdle Formula--->
<br>
<label class="mts-label">TDEE Katch McArdle Formula</label>
<div id="Ktch">
<input type="text" class="mts-field" id="tdee_katch" name="tdee_katch"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---BMR Cunningham Formula--->
<br>
<label class="mts-label">BMR Cunningham Formula</label>
<div id="Cunningham">
<input type="text" class="mts-field" id="bmr_cunningham" name="bmr_cunningham"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---TDEE Cunningham Formula--->
<br>
<label class="mts-label">TDEE Cunningham Formula</label>
<div id="Cunningham">
<input type="text" class="mts-field" id="tdee_cunningham" name="tdee_cunningham"
placeholder="0.000,0 Kcal" maxlength="6" readonly/>
</div>
<!---Calc & Reset Button--->
<br>
<button name="calculate" onclick="calculate()">Re-Calculate</button>
<button id="reset" onclick="resetFields()">Reset</button>
您需要告诉 toLocaleString
您不需要数字:
document.getElementById('tdee_cunningham').value = bmr_cunningham.toLocaleString('it-IT', { maximumFractionDigits: 0 } + " Kcal";