dongzong1866 2012-03-09 17:07
浏览 58
已采纳

表单选择具有最大组合可选值的字段

Hello stackoverflow guru's! This is my first post after having used the site for a fair while now. My normal realm of expertise is within PHP and MySQL so when I have free time I will start to contribute in those areas.

Now my question:

I have a form I am building for a booking system.

One of the fields is a select field to ascertain how many adults and children are being booked for. The are two separate dropdown select fields with several options.

The rules of the booking script are that a maximum of 8 people can stay with a minimum of 1 adult.

Obviously I could put a condition in the processing PHP file, but thats not very good for the user experience so we want to have it that when a number of people is selected from either dropdown, it fades out any value in the other that would take the total over 8.

The HTML would be like this:

<select id="adults">
  <option value="1">1</option>
  <option selected="selected" value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
</select>

<select id="children">
  <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>
  <option value="6">6</option>
  <option value="7">7</option>
</select>

What I am having significant issues with is the jQuery code I will need to fade out the irrelevant options, as it needs to work both ways ie. if they select 5 children first, then adult values 4-8 are then blanked out, or if they select 3 adults first, children values 6 and 7 would blank out.

I would post what I have tried so far with jQuery, but I now know it is completely the wrong code as I was going in the direction of using jQuery calculate.

Any ideas? (Many many thanks and appreciation in advance, I am sorry that I cant provide any more info).

Kevin

  • 写回答

3条回答 默认 最新

  • douya1248 2012-03-09 17:30
    关注

    Try this:

    var cache = {
        $adults:            $('#adults'),
        $children:          $('#children')
    }
    cache.$adultsOptions    = cache.$adults.find('option');
    cache.$childrenOptions  = cache.$children.find('option');
    
    cache.$adults.add(cache.$children).change(function()
    {
        var $this    = $(this),
            other    = $this.is( cache.$adults ) ? '$children' : '$adults',
            max      = 8 - $this.val();
    
        cache[other + 'Options'].each(function()
        {
            $.prop( this, 'disabled', $.attr(this, 'value') > max );
        });
    });
    
    cache.$adults.change();​
    

    Remember to always cache your selectors!!


    Here's the fiddle: http://jsfiddle.net/ZmBLw/

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

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失