使用多个选项的残疾属性限制Svelte表单中的复选框数量
显示项目选项的表格。用户假设为每个项目选择特定数量的选项。选项示例:
let optiongroups = [
{
groupname : "exactly1",
grouplimit : 1,
id : 123,
optionitems : [
{
name : "One",
price : 1
},
{
name : "Two",
price : 2
},
{
name : "Three",
price : 3
},
{
name : "Four",
price : 4
}
]
}
]
使用一组选项,此代码可行:
<form name="optionsform" on:submit|preventDefault={()=>collectoptions(itemid, itemprice, itemname, itemdescription, variationname, variationprice)}>
{#each optiongroups as group}
<div style="margin : 5px; border : solid green;">
<li style="list-style : none;">
{group.groupname} - {group.grouplimit}
{#each group.optionitems as option}
<li>
<label>
<input name={group.groupname} type="checkbox" bind:group={checkresults} value={[{"name" : option.name},{ "price" : option.price } ]} on:click={(e)=>{handleclick(e, group.groupname, group.grouplimit, group.id)}} disabled={disableit === true}>
{option.name} : {option.price}
</label>
</li>
{/each}
</li>
</div>
{/each}
<button type="submit" >Add To cart</button>
</form>
通过在我的脚本中使用禁用来禁用输入复选框,并且确实可以正常工作,但是当我添加更多的选项时,它来自DB,我不得知道每个项目都有多少个选项组 - 因此,当我添加多个选项时:
let optiongroups = [
{
groupname : "exactly1",
grouplimit : 1,
id : 123,
optionitems : [
{
name : "One",
price : 1
},
{
name : "Two",
price : 2
},
{
name : "Three",
price : 3
},
{
name : "Four",
price : 4
}
]
},
{
groupname : "exactly2",
grouplimit : 2,
id : 369,
optionitems : [
{
name : "2-One",
price : 22
},
{
name : "2-Two",
price : 44
},
{
name : "2-Three",
price : 66
},
{
name : "2-Four",
price : 88
}
]
}]
如果您在repl中运行此代码时,您会观察到,当您单击第一组中的选项时 - 正好1-并超过限制您需要选择的选项,该组中的其余复选框将被禁用 - 这是所需的结果 - 但是当您在第二组中启动选择时 - 恰好2--您将选择一个复选框,然后所有其余的所有内容也禁用。我试图以不同的方式尝试移动限制变量,但也没有阻止第二组被禁用。
禁用的行,我尝试添加不同的条件:
disabled={disableit === true && checkresults['id'].includes(group['id'])}
但是,它仍然禁用第二组中的其余复选框。因此,允许用户从两个组中总共检查3个复选框,而不是第一组中的1个选项,第二组中的2个选项。
想知道是否有人可以弄清楚该错误在哪里或将我引向如何包含一组选项,并让用户从每个组中选择正确的选项,然后限制其余的复选框。
A form that displays options for an item. The user suppose to select specific number of options for each item. Options example:
let optiongroups = [
{
groupname : "exactly1",
grouplimit : 1,
id : 123,
optionitems : [
{
name : "One",
price : 1
},
{
name : "Two",
price : 2
},
{
name : "Three",
price : 3
},
{
name : "Four",
price : 4
}
]
}
]
With one group of options, this code works:
<form name="optionsform" on:submit|preventDefault={()=>collectoptions(itemid, itemprice, itemname, itemdescription, variationname, variationprice)}>
{#each optiongroups as group}
<div style="margin : 5px; border : solid green;">
<li style="list-style : none;">
{group.groupname} - {group.grouplimit}
{#each group.optionitems as option}
<li>
<label>
<input name={group.groupname} type="checkbox" bind:group={checkresults} value={[{"name" : option.name},{ "price" : option.price } ]} on:click={(e)=>{handleclick(e, group.groupname, group.grouplimit, group.id)}} disabled={disableit === true}>
{option.name} : {option.price}
</label>
</li>
{/each}
</li>
</div>
{/each}
<button type="submit" >Add To cart</button>
</form>
By using disabled inside my script to disable the input checkbox and it does work correctly but when I add more than group of options - it comes from a db and I don't know how many option groups each item has - so when I add more than one group of options:
let optiongroups = [
{
groupname : "exactly1",
grouplimit : 1,
id : 123,
optionitems : [
{
name : "One",
price : 1
},
{
name : "Two",
price : 2
},
{
name : "Three",
price : 3
},
{
name : "Four",
price : 4
}
]
},
{
groupname : "exactly2",
grouplimit : 2,
id : 369,
optionitems : [
{
name : "2-One",
price : 22
},
{
name : "2-Two",
price : 44
},
{
name : "2-Three",
price : 66
},
{
name : "2-Four",
price : 88
}
]
}]
If you run this code in a repl, you'll observe that when you click on options in the first group - exactly1- and exceed the limit of option you're required to choose, the rest of checkboxes in that group are disabled - which is the desired outcome- but when you start your selection in the second group - exactly2 - you will select one checkbox and then all of the rest get disabled too. I tried to experiment with moving the limitit variable in different ways but nothing is stopping the second group from being disabled too.
The disabled line, I tried to add different condition like :
disabled={disableit === true && checkresults['id'].includes(group['id'])}
But still, it disable the rest of the checkboxes in the second group too. So the user is allowed to check 3 checkboxes in total from the two groups instead of 1 option from the first group and 2 from the second group.
Wonder if anyone could figure out where is the bug or direct me to how to include more than one group of options and let user choose from each group the correct number of options and then restrict the rest of the checkboxes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用所有组的简单全局值,例如
disableIt
;相反,您需要按小组检查状态。例如,如果checkResults
根据此类可用组将其初始化为字典:那么您可以通过以下方式通过:
replot示例
value
设置的方式使它有点冗长。同样,将每个组的整个内容隔离到一个单独的组件中会更容易,这样他们就可以在本地管理自己的状态,并且您不需要使用复杂的状态访问,例如checkResults [group.groupname] 始终。
You cannot use a simple global value like
disableit
for all groups; instead you need to check state by group. E.g. ifcheckresults
is initialized to a dictionary according to the available groups like this:Then you can disable each group individually via:
REPL example
The way that the
value
is set up makes this a bit verbose. Also, it would be easier to isolate the entire content of each group into a separate component, that way they can manage their own state locally and you do not need to use complex state access likecheckresults[group.groupname]
all the time.Example with separate component