如何限制客户的选择


How to restrict the choice of the customer

这是我商店中的商品。每件商品都可以通过五种不同的选项购买。目前,用户可以购买具有所有可能性的商品,我想将他的选择限制为五分之二。请就如何下沉它提出建议,我可以使用php还是我需要使用JavaScript

if($check_item_ok[8] == '0') { // 0 for Weapons Pendants
            if($item_exc >= '1') {echo'
                            <div class="opt_title">Increases Mana After monster +Mana/8</div>
                            <div class="opt"><input id="ex1" onclick="checkall();" ame="boxes" value="1" type="checkbox"></div>
            ';};
            if($item_exc >= '2') { echo'
                            <div class="opt_title">Increases Life After monster +Life/8</div>
                            <div class="opt"><input id="ex2" onclick="checkall();" ame="boxes" value="2" type="checkbox"></div>
            ';};
            if($item_exc >= '3') { echo'
                            <div class="opt_title">Increase attacking(wizardly)speed+7</div>
                            <div class="opt"><input id="ex3" onclick="checkall();" ame="boxes" value="3" type="checkbox"></div>
            ';};
            if($item_exc >= '4') { echo'
                            <div class="opt_title">Increase Damage +2%</div>
                            <div class="opt"><input id="ex4" onclick="checkall();" ame="boxes" value="4" type="checkbox"></div>
            ';};
            if($item_exc >= '5') { echo'
                            <div class="opt_title">Increase Damage +level/20</div>
                            <div class="opt"><input id="ex5" onclick="checkall();" ame="boxes" value="5" type="checkbox"></div>
            ';};
            if($item_exc >= '6') { echo'
                            <div class="opt_title">Excellent Damage Rate +10%</div>
                            <div class="opt"><input id="ex6" onclick="checkall();" ame="boxes" value="6" type="checkbox"></div>
            ';};

你肯定需要在前端(即JavaScript)上执行此操作,因为PHP在提供页面后无法修改内容。您只需要对复选框进行计数,然后首先根据是否选中设置禁用属性,然后根据是否已达到最大值来循环设置禁用属性的输入。在下面的代码片段中,为了方便起见,我只是将逻辑放在您的checkall()函数中(有更好的方法,我不会深入)。你还有一个错别字ame="boxes"应该name="boxes".

const checkall = () => {
  const options = document.querySelectorAll('input[name="boxes"]')
  const max_opts = 2
  let sel_opts = 0
  for (const [i, opt] of options.entries()) {
    sel_opts = sel_opts + Number(opt.checked) // + (0||1)
    if (sel_opts === max_opts) // hit max break loop
      break;
  }
  for (const [i, opt] of options.entries()) {
    // only disable the unchecked when max is hit
    opt.disabled = opt.checked ? false : max_opts > sel_opts ? false : true;
  }
}
<div class="opt_title">Increases Mana After monster +Mana/8</div>
<div class="opt"><input id="ex1" onclick="checkall();" name="boxes" value="1" type="checkbox"></div>
<div class="opt_title">Increases Life After monster +Life/8</div>
<div class="opt"><input id="ex2" onclick="checkall();" name="boxes" value="2" type="checkbox"></div>
<div class="opt_title">Increase attacking(wizardly)speed+7</div>
<div class="opt"><input id="ex3" onclick="checkall();" name="boxes" value="3" type="checkbox"></div>
<div class="opt_title">Increase Damage +2%</div>
<div class="opt"><input id="ex4" onclick="checkall();" name="boxes" value="4" type="checkbox"></div>
<div class="opt_title">Increase Damage +level/20</div>
<div class="opt"><input id="ex5" onclick="checkall();" name="boxes" value="5" type="checkbox"></div>
<div class="opt_title">Excellent Damage Rate +10%</div>
<div class="opt"><input id="ex6" onclick="checkall();" name="boxes" value="6" type="checkbox"></div>