duanci1858 2012-02-16 09:55
浏览 49
已采纳

表单中包含单词的开/关状态?

Doing a rebuild of a website and I want to give several words an "on" and "off" state within a form

The previous developer created this site but looking at his code it's slightly horrific - http://www.mediwales.com/news . I'm doing the bit in the grey box where the sectors can be selected.

How can I recreate this? Is there a more modern way to do this in jquery? I need it to work across all the browsers (down to IE7).

I want to be able to style the on/off states and tie it to the form.

  • 写回答

3条回答 默认 最新

  • dqv2743 2012-02-16 10:31
    关注

    you can try either a library such as wijmo (check out http://wijmo.com/demo/explore/#formdecorator|overview; you'll only need to draw a rectangle around it and alter the style). Or as a quick and dirty solution do something like:

    CSS:

    .check { display: block; border: 1px solid #000; }
    .check.selected { border: 2px solid red; }
    

    HTML:

    <div class="check" id="biotech-id">Biotechnology</div>
    <div class="check" id="financial-id">financial</div>
    

    jQuery:

    $('.check').on('click', function(e) {
        el = $(e.currentTarget);
        el.toggleClass('selected');
        if(el.hasClass('selected')) {
            input = $('<input type="hidden" name="checkedSectors[]" value="'+ el.attr('id') + '" id="hiddeninput-'+ el.attr('id') +'" />');
            el.after(input);
        } else {
            $('#hiddeninput-'+ el.attr('id')).remove();
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?