开发者

When press submit button, check if checkbox is selected

开发者 https://www.devze.com 2023-04-01 02:22 出处:网络
I tried \"is\" function but it is not work in asp.net. I just want to check if the box is select, if not keep in this page and show notice message.

I tried "is" function but it is not work in asp.net. I just want to check if the box is select, if not keep in this page and show notice message.

Code:

 @foreach (var f in gs.Set.Facets)
         {
            <li>
               <label>
                  <input type="@(gs.Set.AllowMultipleSelection ? "checkbox" : "radio")" 
                     name="FacetResults" 
                     value="@f.FacetID"@(ViewData.Model.FacetStates[f.FacetID] ? " checked=\"checked\"" : "")  class="sel"/>
                     @f.LabelText
               </label>
            </li>
         }
   <inpu开发者_开发知识库t type="submit" value="Complete" id = "Complete" />


Try this

$("#Complete").click(function(){

  if(!$("input:checkbox[name='FacetResults']").is(":checked")){
      return false;
  }

});


It depends on where you want that logic. Shankar's answer is great if you want the validation explicitly in the javascript, however I tend to prefer a ViewModel with Data Annotations approach.


Use jQuery's .each() function to check each checkbox's state:

$('#Complete').click(function () {

    var isSelected = false;

    $('input:checkbox[name="FacetResults"]').each(function (index) {
        if ($(this).attr('checked')) {
            isSelected = true;
        }
    });

    alert(isSelected);
    return isSelected;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号