我需要能够在使用JavaScript的论坛上为我的数量字段设置最大值和最小值。 iv尝试添加最大和最小值,并且iv在网上看了一个教程,但是它们似乎对我没有用(除非即时通讯做错了,因为这是JavaScript的新手,所以很有可能)
我的HTML /表单代码:
<form action="https://titan.csit.rmit.edu.au/~e54061/wp/processing.php? ref=product" method="get">
Game Of Thrones Season 1
<input type = "hidden" name = "id" value = "M01" />
<br>
<select name="option">
<option value="HD">HD</option>
<option value="Full HD">Full HD</option>
<option value="Blu-ray">Blu-ray</option>
</select>
<div class="widthc">
<button id="minus">−</button>
<input type="number" name="qty" value="0" id="qty"/>
<button id="plus">+</button>
<button class="button" type="submit">Submit</button>
</form>
我的Javascript代码:
<script>
const minusButton = document.getElementById('minus');
const plusButton = document.getElementById('plus');
const inputField = document.getElementById('qty');
minusButton.addEventListener('click', event => {
event.preventDefault();
const currentValue = Number(inputField.value) >> 0;
inputField.value = currentValue - 1;
});
plusButton.addEventListener('click', event => {
event.preventDefault();
const currentValue = Number(inputField.value) >> 0;
inputField.value = currentValue + 1;
});
</script>
答案 0 :(得分:0)
获取数字字段允许的最大值:
var x = document.getElementById("myNumber").max;
更改最大值:
document.getElementById("myNumber").max = "10";
答案 1 :(得分:0)
我为每个EventListener函数添加了一个if
const minusButton = document.getElementById('minus');
const plusButton = document.getElementById('plus');
const inputField = document.getElementById('qty');
var currentValue = 0;
minusButton.addEventListener('click', event => {
event.preventDefault();
currentValue = Number(inputField.value) >> 0;
currentValue = currentValue - 1;
if (currentValue < 0) {
inputField.value = 0;
} else {
inputField.value = currentValue;
}
});
plusButton.addEventListener('click', event => {
event.preventDefault();
currentValue = Number(inputField.value) >> 0;
currentValue = currentValue + 1;
if (currentValue > 10) {
inputField.value = 10;
} else {
inputField.value = currentValue;
}
});
Game Of Thrones Season 1
<input type="hidden" name="id" value="M01" />
<br>
<select name="option">
<option value="HD">HD</option>
<option value="Full HD">Full HD</option>
<option value="Blu-ray">Blu-ray</option>
</select>
<div class="widthc">
<button id="minus">−</button>
<input type="number" name="qty" value="0" id="qty" />
<button id="plus">+</button>
<button class="button" type="submit">Submit</button>
答案 2 :(得分:0)
您可以将min
和max
设置为输入字段并签入JavaScript代码。
const minusButton = document.getElementById('minus');
const plusButton = document.getElementById('plus');
const inputField = document.getElementById('qty');
var currentValue = 0;
minusButton.addEventListener('click', event => {
event.preventDefault();
currentValue = Number(inputField.value) >> 0;
currentValue = currentValue - 1;
if (currentValue < inputField.min) {
inputField.value = inputField.min;
} else {
inputField.value = currentValue;
}
});
plusButton.addEventListener('click', event => {
event.preventDefault();
currentValue = Number(inputField.value) >> 0;
currentValue = currentValue + 1;
if (currentValue > inputField.max) {
inputField.value = inputField.max;
} else {
inputField.value = currentValue;
}
});
<form action="https://titan.csit.rmit.edu.au/~e54061/wp/processing.php? ref=product" method="get">
Game Of Thrones Season 1
<input type = "hidden" name = "id" value = "M01" />
<br>
<select name="option">
<option value="HD">HD</option>
<option value="Full HD">Full HD</option>
<option value="Blu-ray">Blu-ray</option>
</select>
<div class="widthc">
<button id="minus">−</button>
<input type="number" name="qty" value="0" id="qty" min="0" max="10" />
<button id="plus">+</button>
<button class="button" type="submit">Submit</button>
</form>