如何从javascript中的复选框获取值以检查值是True还是False?

时间:2017-02-22 07:46:30

标签: javascript c#

我想从javascript中的复选框获取值,检查值是True还是False,如果“False”我想要show popup确认“是/否?”。

的javascript

function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";

    var isChecked = $('#chkSome').is(':checked');

    if (isChecked) {

    }
    else {
        if (confirm("Yes/No?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value); ;
    }      
}

C#(GridView中的TemplateField)

<asp:TemplateField HeaderText="Some Pcs">
   <ItemTemplate>
         <asp:CheckBox runat="server" ID="chkSome" Width="20px" AutoPostBack="true" onclick="Confirm();" OnCheckedChanged="chkSome_OnCheckedChanged" />
         <asp:Label runat="server" ID="lblQty" Style=" padding-right:2px;" Width="48px" Text='<%# Bind("Quantity") %>'></asp:Label>
         <asp:Label runat="server" ID="lblCSQty" Text='<%# Bind("CSQty") %>' Visible="false"></asp:Label>
   </ItemTemplate>
</asp:TemplateField>

C#(检查更改后的代码)

protected void chkSome_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;
        GridViewRow rowindex = chk.NamingContainer as GridViewRow;

        CheckBox chkSome = (CheckBox)GridView3.Rows[rowindex.RowIndex].FindControl("chkSome");

        if (chkSome.Checked)
        {
            //do something
        }
        else
        {
            confirmValue = Request.Form["confirm_value"];

            if (confirmValue == "Yes")
            {
                //do something
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

try this . 

   $('#chkSome').click(function(){
        var checked = $(this).is(':checked');
        if(checked) {
            if(!confirm('Are you sure ?')){         
                $(this).removeAttr('checked');
            }
        } else if(!confirm('Are you sure ?')){
            $(this).attr("checked", "checked");
        }
    }​);​

以下是JSFiddle工作示例

https://jsfiddle.net/73w19L9k/

答案 1 :(得分:0)

如果javascript可以尝试chkSome.checked

像这样

var chkSome = document.getElementById('chkSome');
var result = document.getElementById('result');

function testCheck() {

  if(chkSome.checked){
    result.innerHTML = "checked!";
  }else{
    result.innerHTML = "unchecked!";
  }
  
}
<input id="chkSome" type="checkbox" onclick="testCheck()">
<div id="result">↑click here</div>

希望帮助

答案 2 :(得分:0)

您可以尝试使用此代码,它对我来说就像是一种魅力。它将返回一个truefalse

$("input[title=chkSome]").is(":checked")