【问题标题】:Checkbox is checked or not in mvc复选框是否在mvc中被选中
【发布时间】:2017-07-12 19:39:39
【问题描述】:
$("#Month option:selected").val();

通过这个我们可以在我们的 DDL 中获得选择的值。与此相同,我们如何获取天气 CheckBox 是否被选中。我该怎么做?

<input id="Baruc" type="checkbox" value="true" name="Baruc" data-val-required="The Baruc field is required." data-val="true" onchange="showlabel();">

这个复选框?

【问题讨论】:

标签: jquery model-view-controller checkbox checked


【解决方案1】:

解决方案 1:

您需要将.length 用于选中的元素。

if($("#Baruc:checked").length){
  //checkbox is checked
}

解决方案 2:

您也可以将.is() 方法与:checked 选择器一起使用:

if($("#Baruc").is(":checked")){
  //checkbox is checked
}

【讨论】:

    【解决方案2】:

    使用这个

    $('#Baruc').is('checked');
    

    【讨论】:

      【解决方案3】:

      您可以在 onchange 函数中传递this

      onchange="showlabel(this);
      

      现在在你的方法中你可以使用这个:

      function showlabel(elem){
          alert(elem.checked); // true if checked false if unchecked
      }
      

      function showlabel(elem){ alert(elem.checked);}
      &lt;input id="Baruc" type="checkbox" value="true" name="Baruc" data-val-required="The Baruc field is required." data-val="true" onchange="showlabel(this);"&gt;

      【讨论】:

        【解决方案4】:

        为了检查复选框是否被选中,您可以尝试使用.is(),如图所示:-

        jQuery 语法。

        $('#Baruc').is(':checked') // it returns bool if checkbox is checked it will return 'true' otherwise 'false'.
        

        编辑:-检查前必须有冒号

        Javascipt 语法。

        if(document.getElementById("Baruc").checked)
        {
           //checkbox is checked
        }
        

        【讨论】:

          【解决方案5】:

          您可以通过给定的代码检查复选框是否被选中:

          $(‘#CHECKBOXID’).is(‘:checked’);
          

          如果有任何混淆,请使用以下链接:

          https://codeupweb.wordpress.com/2017/07/11/check-if-checkbox-if-checked-using-jquery/

          【讨论】:

            猜你喜欢
            • 2012-06-08
            • 2021-07-16
            • 1970-01-01
            • 1970-01-01
            • 2015-12-28
            • 2020-03-01
            • 2019-07-04
            相关资源
            最近更新 更多