代码之家  ›  专栏  ›  技术社区  ›  Antonio

使引导选择下拉列表是必需的

  •  1
  • Antonio  · 技术社区  · 6 年前

    我的表格中有一个下拉列表 bootstrap-select required 内部 <select>

    <div class="form-group">
        <label for="location">Location</label>
        <select name="location" id="location-form" required>
           <option value="">Choose Location</option>
           <?php foreach($location as $row) : ?>
           <option value="<?=$row['location_id']?>"><?=$row['location_name']?></option>
           <?php endforeach; ?>
        </select>
    </div>
    

    5 回复  |  直到 6 年前
        1
  •  1
  •   bronkula    6 年前

    select总是有一个值。试着用无线电组代替。

    HTML5: How to use the "required" attribute with a "radio" input field

        2
  •  1
  •   Niket Joshi    6 年前

    下面有两个方法可以帮助我和你分享。

    用这种方法也可以解决问题。

    <form action="whatever" method="post" novalidate>
    

    /* For checking the drop-down contain value of not use this. */ 
    if(jQuery('#location-form').val() == ''){ 
       alert('Please select the option for location'); 
    }else{ 
      return false; 
    }
    

    我希望这对你有帮助。 谢谢。

        3
  •  0
  •   Ayush Jain    6 年前

    <div class="form-group"> <label for="location">Location</label> <select name="location" id="location-form" required> <option value="" disabled>Choose Location</option> <?php foreach($location as $row) : ?> <option value="<?=$row['location_id']?>"><?=$row['location_name']?></option> <?php endforeach; ?> </select> </div>
    
        4
  •  0
  •   Tony    4 年前

    我是一个新的编码,但我做了大量的研究,在这个网站上,我发现了一个解决方案(不是那么有效,但工作)。这个解决方案基于这样一个事实:html5的“required”属性至少在某种程度上起了作用—它阻止您继续下一步或提交,但除此之外没有其他功能—例如,没有错误消息,没有警报,什么都没有。

    $('#button').click(function () {
      $('#id-error').hide(); //if there is indeed a html5 message showing, but it looks bad, then just hide it, if you inspect the msg, html has automatically generate this "id-error" id 
      if ($('#id').is(':disabled')){
          $('#errormsg-id').removeClass('d-block'); 
      } else if (!$('#id').val()){
          $('#errormsg-id').addClass('d-block');
      };
    });
    
    $('#id').change(function () {
      if($('#id').val()){
          $('#errormsg-id').removeClass('d-block');
      };
    });
    
    <div class="form-group ">
    <select id="id" class="selectpicker" required>
    <option >...</option>
    </select>
    <div id="errormsg-id" class="invalid-feedback ">Please make a selection.</div>
    </div>
    
        5
  •  -1
  •   SO19    6 年前

    你把你的select放在表单标签上了吗? 请参考- https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_select_required

        6
  •  -1
  •   Kishan    6 年前

    如果需要,您可以添加java脚本验证,如下所示,并用您的表单标签id替换“FormnameId”。

    <script type="text/javascript">
    $(document).on('submit','form#FormnameId',function(event){
    	event.preventDefault();
    	var sel = $("#location-form").val();
    	if(sel == ''){
    		alert('Please select value first');
    		return false;
    	}
    });
    </script>
    推荐文章