doupijin0397 2017-04-22 20:54
浏览 286
已采纳

多个复选框 - 更改消息“如果要继续,请选中此框

I have a multiselect checkbox form to which I add a JS to make sure the visitor selects at least one option

<div class="form-group options">
<label class="control-label col-md-4" for="optiontext">Specify an option</label>
<div class="col-md-6">
    <input type="checkbox" name="option[]" value="option1" required/> Option 1<br>
    <input type="checkbox" name="option[]" value="option2" required/> Option 2<br>
    <input type="checkbox" name="option[]" value="option3" required/> Option 3<br>
    <input type="checkbox" name="option[]" value="option4" required/> Option 4<br>
    <input type="checkbox" name="option[]" value="option5" required/> Option 5<br>
    <input type="checkbox" name="option[]" value="option6" required/> Option 6<br>
    <input type="checkbox" name="option[]" value="option7" required/>  Option 7
</div>

That's the JS

$(function(){
var requiredCheckboxes = $('.options :checkbox[required]');
requiredCheckboxes.change(function(){
    if(requiredCheckboxes.is(':checked')) {
        requiredCheckboxes.removeAttr('required');
    } else {
        requiredCheckboxes.attr('required', 'required');
    }
});

Now ALL IS WORKING fine, the only thing is that the error message that comes up is Please check this box if you want to proceed but I would like the message to be Please select at least one option to proceed like illustrated on the picture here.

enter image description here

How do I go about it?

  • 写回答

1条回答 默认 最新

  • douleng3463 2017-04-22 21:32
    关注

    It's very simple to control custom messages with the help of HTML5 event oninvalid

    Code:

    <input type="checkbox" value="option1"
       oninvalid="this.setCustomValidity('Please select at least one option to proceed')">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题