2017-02-22 65 views
-1

我想從javascript中的複選框中獲取值,以檢查值是True還是False,如果「False」我想顯示彈出確認「Yes/No?」。如何從javascript複選框獲取值爲校驗值是True還是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)

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

回答

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/

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>

希望幫助

+0

微軟JScript運行時錯誤:無法獲取財產「的價值檢查':object是null或undefined。它在點擊chckckbox時顯示此消息。 – SueSaya

相關問題