繁体   English   中英

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

[英]How to get value from checkbox in javascript for check value is True or False?

我想从javascript的复选框中获取值,检查值是True还是False,如果“ False”我想显示弹出窗口确认“是/否?”。

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
            }
        }
    }
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/

如果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> 

希望帮助

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM