2013-11-10 36 views
0

我想禁用並啓用刪除按鈕,如果複選框沒有在GridView中由Jquery檢查? 這是我的代碼不起作用。如果在JQuery中沒有在GridView中選中複選框,如何禁用並啓用刪除按鈕?

請以簡單的方式幫助我,因爲我是一名新開發人員!

<script type="text/javascript"> 

        $(document).ready(function() { 

         $('#<%=GridView1.ClientID %>').find('input[Id*="CheckBox1"]:checkbox').click(function() { 
          if (this.checked ==true) 
          { 
           $('[id$="Button2"]').attr('disabled',false); 

          } 
          else 
          { 
           $('[id$="Button2"]').attr('disabled',true); 
          } 
         }) 
        }); 
     </script> 
+0

使用CSS類操作DOM對象..在數據綁定控件的情況下.. –

回答

1

的語法檢查,如果複選框被選中 -

<input id="checkbox" type="checkbox" name="one" value="1" checked="checked"> 

-

// First method - Recommended 
$('#checkbox').prop('checked') // Boolean true 

// Second method - Makes code more readable (e.g. in if statements) 
$('#checkbox').is(':checked') // Boolean true 

// Third method - Selecting the checkbox & filtering by :checked selector 
$('#checkbox:checked').length // Integer >0 
$('#checkbox:checked').size() // .size() can be used instead of .length 

// Fourth method - Getting DOM object reference 
$('#checkbox').get(0).checked // Boolean true 
$('#checkbox')[0].checked  // Boolean true (same as above) 
0

這裏是JavaScript,你應該使用:

$('#<%=GridView1.ClientID %> tr td input[id*="CheckBox1"][type=checkbox]').click(function() { 
    var btn = $(this).closest('tr').find('td input[id*="Button2"]'); 
    if ($(this).prop("checked") === true) { 
     btn.attr("disabled", false); 
    } else { 
     btn.attr("disabled", true); 
    } 
}); 

的問候, Uros

+0

我一定要打電話這個函數在客戶端點擊? – 2017-05-21 05:07:17

0

在GridView複選框試試這個

function onCheckBoxChecked(obj) { 
    var btn = $(obj).closest('tr').find('input[id*="Button2"]'); 
    var isChecked = $(obj).attr('checked') ? true : false; 
    if (isChecked) 
    btn.attr("disabled", false); 
else 
    btn.attr("disabled", true); 
} 

,並添加onclick事件

<asp:CheckBox ID="chkBox" onclick="onCheckBoxChecked(this);" runat="server"> 
           </asp:CheckBox> 
+0

這將在網格中工作嗎? – 2017-05-21 05:18:02

+0

http://stackoverflow.com/questions/44092743/how-to-disable-a-button-when-all-the-checkboxes-in-grid-are-disable-or-un-checke/44092853?noredirect=1 #comment75207474_44092853 – 2017-05-21 05:28:32

相關問題