英文:
Select option with a max value
问题
如果我选择了"Item one",我希望输入数字的最大允许值是3;如果选择了"Item two",则最大允许值为4。我尝试使用foreach循环进行循环,但没有返回正确的结果。
英文:
Let's say I have a select with some option values
<select name="select">
<option value="item-one">Item one</option>
<option value="item-two">Item two</option>
</select>
<input type="number" name="quantity">
If I selected Item one, I want the input number to have a max allowed value of 3, otherwise if Item two if selected it would be 4.
I tried looping it using foreach loop but wont return the right result
答案1
得分: 1
你不能在PHP中这样做,但可以使用JavaScript。
我使用一个数组,其中索引与选择(select)的selectedIndex匹配。
我在加载时触发,并且如果选择(select)发生更改,我也会减少。
我还添加了一个输入事件处理程序,因为最小值和最大值只适用于微调按钮和提交事件。
对于我的输入测试的替代方法是在输入字段上设置并触发自定义有效性(custom validity)。
window.addEventListener("DOMContentLoaded", () => {
const sel = document.querySelector("[name=select]");
const numberField = document.querySelector("[name=quantity]");
sel.addEventListener("change", (e) => {
numberField.max = [3, 4][e.target.selectedIndex];
if (+numberField.value > +numberField.max) numberField.value = numberField.max;
});
sel.dispatchEvent(new Event('change')); // 初始值
numberField.addEventListener("input",(e) => {
const val = numberField.value;
if (+val > +numberField.max) numberField.value = numberField.max;
})
numberField.addEventListener("blur",(e) => {
const val = numberField.value;
if (isNaN(parseFloat(val)) || !isFinite(val)) numberField.value = 0; // 如果没有数字,则在失去焦点时删除[e.-+]字段允许
})
});
<select name="select">
<option value="item-one">项目一</option>
<option value="item-two">项目二</option>
</select>
<input type="number" name="quantity">
英文:
You cannot do that in PHP but with JavaScript you can.
I use an array where the index matches the selectedIndex of the select
I trigger on load and I also reduce if the select is changed
I added an input event handler too, because min/max only works with the spinners AND the submit event.
Alternative for my input test is to set and trigger custom validity on the input field
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
window.addEventListener("DOMContentLoaded", () => {
const sel = document.querySelector("[name=select]");
const numberField = document.querySelector("[name=quantity]");
sel.addEventListener("change", (e) => {
numberField.max = [3,4][e.target.selectedIndex];
if (+numberField.value > +numberField.max) numberField.value = numberField.max;
});
sel.dispatchEvent(new Event('change')); // initial value
numberField.addEventListener("input",(e) => {
const val = numberField.value;
if (+val > +numberField.max) numberField.value = numberField.max;
})
numberField.addEventListener("blur",(e) => {
const val = numberField.value;
if (isNaN(parseFloat(val)) || !isFinite(val)) numberField.value = 0; // field allows [e.-+] remove on blur if no digits
})
});
<!-- language: lang-html -->
<select name="select">
<option value="item-one">Item one</option>
<option value="item-two">Item two</option>
</select>
<input type="number" name="quantity">
<!-- end snippet -->
答案2
得分: 0
你可以使用这个<select id="selectItem" name="select">
<option value="item-one">项目一</option>
<option value="item-two">项目二</option>
</select>
<input id="quantity" type="number" name="quantity">
<script>
document.getElementById('selectItem').addEventListener('change', function(e) {
var quantityInput = document.getElementById('quantity');
if (e.target.value === 'item-one') {
quantityInput.max = 3;
} else if (e.target.value === 'item-two') {
quantityInput.max = 4;
}
});
</script>
英文:
You can use this <select id="selectItem" name="select">
<option value="item-one">Item one</option>
<option value="item-two">Item two</option>
</select>
<input id="quantity" type="number" name="quantity">
<script>
document.getElementById('selectItem').addEventListener('change', function(e) {
var quantityInput = document.getElementById('quantity');
if (e.target.value === 'item-one') {
quantityInput.max = 3;
} else if (e.target.value === 'item-two') {
quantityInput.max = 4;
}
});
</script>
答案3
得分: 0
PHP在服务器端运行,所以除非你向服务器发送命令,否则无法执行此操作。在JavaScript中执行这个操作会更容易。
虽然这不是一个好的方法,但你可以尝试像这样做,以便有个思路:你可以使用JavaScript的onselect函数向PHP端发起一个AJAX请求。你需要在PHP端创建一个验证函数,并获取从onselect发送过来的数据。在验证完成后,例如你发送了"Item One",PHP验证器将返回3。
在JavaScript的success方法中,你将获取返回的数据,然后再次使用JavaScript将其设置为输入类型的最大值。
如果你需要在后台进行复杂的计算而不仅仅是3或4,这可能是你寻找的方法。但如果你只需要3或4这样简单的东西,那就使用JavaScript吧。
英文:
Php works on the server-side so you cant do that unless you send a command to the server. Its much easier to do in javascript.
Its not a good approach, but you can do something like this, to give you an idea, you can make an ajax request to the php side with javascript's onselect function. You will make a validator function on the php side and you will retrieve the data sent from onselect. After the validations, example you sent "Item One" , php validator will return you 3.
At the success method of javascript, you will take the returned data and set it to the input type as the max value with javascript again.
If there is complicated calculations that you want to make at the behind rather than this 3,4 this could be a way you are looking for. But if you just need 3,4 or simple things like that, just go with the javascipt.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论