dongxun7301 2011-08-18 09:50
浏览 195
已采纳

选择多个单选按钮(通过jquery,javascript?)

I currently have a form with lets say 5 radio button entries (see below). What im looking archive is the following: - Being able to choose multiple radio buttons - lets say 3 and submit the form.

Currently I got it working fine with PHP, SQL, but im only able to choose one radiobutton and submit that.

I figure it would also come in handy being able to deselect a radio button in case you wrongly click one.

My guess is that this can be done through some javascript? Any suggestions? Online examples perhaps?

<form id="pollform" action="poll.php" method="post">
<input id="option-1" type="radio" value="1" name="poll">
<label for="option-1">Select option 1</label>

<input id="option-2" type="radio" value="2" name="poll">
<label for="option-2">Select option 2</label>

<input id="option-3" type="radio" value="3" name="poll">
<label for="option-3">Select option 3</label>

<input id="option-4" type="radio" value="4" name="poll">
<label for="option-4">Select option 4</label>

<input id="option-5" type="radio" value="5" name="poll">
<label for="option-5">Select option 5</label>
</form>
  • 写回答

3条回答 默认 最新

  • dpglo66848 2011-08-18 09:53
    关注

    As it has same name poll you will not be able to do that as input type radio is specialized in selecting a single value from multiple inputs.

    You can use input type checkbox for that and make them as an array:

    <form id="pollform" action="poll.php" method="post">
        <input id="option-1" type="checkbox" value="1" name="poll[]">
        <label for="option-1">Select option 1</label>
    
        <input id="option-2" type="checkbox" value="2" name="poll[]">
        <label for="option-2">Select option 2</label>
    
        <input id="option-3" type="checkbox" value="3" name="poll[]">
        <label for="option-3">Select option 3</label>
    
        <input id="option-4" type="checkbox" value="4" name="poll[]">
        <label for="option-4">Select option 4</label>
    
        <input id="option-5" type="checkbox" value="5" name="poll[]">
        <label for="option-5">Select option 5</label>
    </form>
    

    LIMIT (with jQuery) the number:

    $("input[type=checkbox][name=poll[]]").click(function() {
        var numberSel = $("input[type=checkbox][name=poll[]]:checked").length >= 3;     
        $("input[type=checkbox][name=poll[]]").not(":checked").attr("disabled",numberSel);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向