简体   繁体   English

如何在反应中将复选框的数量限制为一定数量?

[英]How to limit number of checkboxes to certain number in react?

  1. This is the code for the checkboxes and I would like to limit the choices to 2.这是复选框的代码,我想将选择限制为 2。
  2. This is going to be used for a pizza application, so the limit number will be different for the different sections in creating a pizza.这将用于比萨饼应用程序,因此在创建比萨饼时,不同部分的限制数会有所不同。
const LagDinEgen = () => {
    return(
   <div>
        <form name="størrelse">
            <div className="desktop:w-3/4 tablet:w-11/12 w-full mx-auto grid grid-cols-1 tablet:grid-cols-2 desktop:grid-cols-3 rounded-lg overflow-hidden desktop:mt-8 gap-2">
                <div className="col-span-1 tablet:col-span-2 desktop:col-span-3 bg-mainBlue py-10 h-auto text-center text-white flex flex-col justify-center items-center">
            
                    <h1 class="mb-6 pt-6 mx-auto text-center"> VELG STØRRELSE</h1>
                    <div class="mx-auto max-w-sm text-center flex flex-wrap justify-center">

                        <div class="flex items-center mr-4 mb-4">
                            <input id="radio1" type="checkbox" name="radio" class="hidden"/>
                            <label for="radio1" class="flex items-center cursor-pointer px-3">
                            <span class="w-4 h-4 inline-block mr-1 border border-mainGreen rounded-full"></span>
                            SMALL</label> <label>129,-</label>
                        </div>

                        <div class="flex items-center mr-4 mb-4">
                            <input id="radio2" type="checkbox" name="radio" class="hidden" />
                            <label for="radio2" class="flex items-center cursor-pointer px-3">
                            <span class="w-4 h-4 inline-block mr-1 border border-mainGreen rounded-full"></span>
                            MEDIUM</label> <label>159,-</label>
                        </div>

                        <div class="flex items-center mr-4 mb-4">
                            <input id="radio3" type="checkbox" name="radio" class="hidden" />
                            <label for="radio3" class="flex items-center cursor-pointer px-3">
                            <span class="w-4 h-4 inline-block mr-1 border border-mainGreen rounded-full"></span>
                            LARGE</label> <label>189,-</label>
                        </div>
                    </div>
                </div>
            </div>
        </form>

Assuming you plan on adding a button, you can utilize the native HTML5 disabled property on it and maintain the state of which items are currently selected to determine when it should be disabled(eg. an array of selectedItems )假设您计划添加一个按钮,您可以在其上使用本机 HTML5 disabled属性并维护当前选择哪些项目的 state 以确定何时应该禁用它(例如selectedItems数组)

You would pass the selectedItems.length > 2 to the disabled property, so it would effectively become disabled if your array is larger than 3.您将selectedItems.length > 2传递给 disabled 属性,因此如果您的数组大于 3,它将有效地被禁用。

Working example: https://codesandbox.io/s/xenodochial-currying-eb8kt?file=/src/Form.js工作示例: https://codesandbox.io/s/xenodochial-currying-eb8kt?file=/src/Form.js

You should pass necessary datas to LagDinEgen component as props and then you should use array.map() function inside of JSX.您应该将必要的数据作为道具传递给 LagDinEgen 组件,然后您应该在 JSX 内部使用 array.map() function。 This will be something gonna like that;这将是这样的;

const LagDinEgen = ({title,options}) => {
    return(
   <div>
        <form name="størrelse">
            <div className="desktop:w-3/4 tablet:w-11/12 w-full mx-auto grid grid-cols-1 tablet:grid-cols-2 desktop:grid-cols-3 rounded-lg overflow-hidden desktop:mt-8 gap-2">
                <div className="col-span-1 tablet:col-span-2 desktop:col-span-3 bg-mainBlue py-10 h-auto text-center text-white flex flex-col justify-center items-center">
                    <h1 class="mb-6 pt-6 mx-auto text-center"> {title} </h1>
                    <div class="mx-auto max-w-sm text-center flex flex-wrap justify-center">
                        {
                            options.map((option,index)=>(
                                <div class="flex items-center mr-4 mb-4">
                                    <input type="checkbox" name="radio" class="hidden"/>
                                    <label class="flex items-center cursor-pointer px-3">
                                    <span class="w-4 h-4 inline-block mr-1 border border-mainGreen rounded-full"></span>
                                    {option.name}</label> <label>{option.price},-</label>
                                </div>
                            ))
                        }
                    </div>
                </div>
            </div>
        </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM