duadpnld426905 2014-09-17 16:42
浏览 532
已采纳

使用select标签进行选择后禁用选项?

I'm building a web form and I currently have five 'drop-down' menus (using the select tag) that allow users to choose a value from 0-5. It looks like this:

              Choose one:
                    <select name="choose_one" required>
                        <option value=""></option>
                        <option value="0">0</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>    
                    </select>

Is there anything I can do to disable a certain number from being chosen again if it is chosen on a select menu in other menus? Meaning, if I select 1 on the first drop-down menu I come across, I wouldn't be able to select it again on the others.

A potential solution I was considering was making a table of radio buttons. When a user selects a number, it disables that whole row of numbers. However I'm not really sure if I could do this in HTML. I think I'd need to use JS/JQuery.

Test table!

One of the issues here is that 0 should be able to be selected multiple times, but a column needs to be disabled after a value for that column has been selected. The 0 field could be checkboxes, but then the style wouldn't be the same as the radio buttons.

  • 写回答

2条回答 默认 最新

  • dpxbc88022 2014-09-17 16:50
    关注

    Use same name for radio buttons in one cloumn.

    For example

    <table>
      <tr>
        <td></td>
        <td>item 1</td>
        <td>item 2</td>
        <td>item 3</td>
      </tr>
      <tr>
        <td>0</td>
        <td><input type="radio" name="item1" value="0" /></td>
        <td><input type="radio" name="item2" value="0" /></td>
        <td><input type="radio" name="item3" value="0" /></td>
      </tr>
      <tr>
        <td>1</td>
        <td><input type="radio" name="item1" value="1" /></td>
        <td><input type="radio" name="item2" value="1" /></td>
        <td><input type="radio" name="item3" value="1" /></td>
      </tr>
      <tr>
        <td>2</td>
        <td><input type="radio" name="item1" value="2" /></td>
        <td><input type="radio" name="item2" value="2" /></td>
        <td><input type="radio" name="item3" value="2" /></td>
      </tr>
    </table>

    Update

    $("input[type=radio]").click(function() {
      var selectedValue = $(this).val() ;
      $("input[value="+selectedValue+"]").each(function(){
        if(!$(this).is(':checked')) {
          $(this).prop('disabled', true);
       }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
          <tr>
            <td></td>
            <td>item 1</td>
            <td>item 2</td>
            <td>item 3</td>
          </tr>
          <tr>
            <td>0</td>
            <td><input type="radio" name="item1" value="0" /></td>
            <td><input type="radio" name="item2" value="0" /></td>
            <td><input type="radio" name="item3" value="0" /></td>
          </tr>
          <tr>
            <td>1</td>
            <td><input type="radio" name="item1" value="1" /></td>
            <td><input type="radio" name="item2" value="1" /></td>
            <td><input type="radio" name="item3" value="1" /></td>
          </tr>
          <tr>
            <td>2</td>
            <td><input type="radio" name="item1" value="2" /></td>
            <td><input type="radio" name="item2" value="2" /></td>
            <td><input type="radio" name="item3" value="2" /></td>
          </tr>
        </table>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序