2010-11-24 26 views
0

我正在創建一個程序,其中每個GridView行中都有一個複選框和一個文本框,它們分別未選中和禁用。當勾選複選框時,我需要啓動一些JavaScript來啓用該文本​​框,反之亦然。到目前爲止,我這樣做:用於GridView行的JavaScript TemplateFields

JS:

<script type="text/javascript"> 
    function onholdev(index) { 
     var chk = document.getElementById('<%=grdCons.Rows[index].FindControl("chkHold").ClientID %>'); 
     var txt = document.getElementById('<%=grdCons.Rows[index].FindControl("txtReason").ClientID %>'); 

     if (chk.checked == true) { 
      txt.disabled = false; 
     } 
     else { 
      txt.disabled = true; 
      txt.value = ""; 
     } 
    } 
</script> 

C#(RowDataBound事件)

  CheckBox chkHold = ((CheckBox)e.Row.FindControl("chkHold")); 
     chkHold.Attributes.Add("onchange", "onholdev(" + e.Row.RowIndex + ")"); 

但在JS,它說的 '索引' 變量不存在,在函數的第一行(以var chk開頭)。我做對了嗎?

回答

-1

非常感謝您的幫助,我通過不使用服務器而是「解決」它。懶惰,但節省了很多JS頭痛。

0

問題是,指數裏面的字符串,而應該是一個參數,這個修復它:

var chk = document.getElementById('<%=grdCons.Rows[' + index + '].FindControl("chkHold").ClientID %>'); 

了同樣適用於其他行

+0

感謝哥們,它告訴我'字符串中的字符太多了',儘管 – Chris 2010-11-24 11:54:44

0

由於該複選框會呈現爲輸入我們可以通過使用$('.check input')找到它,並且由於它是動態添加的,因此我們需要使用jquery bind將點擊功能添加到複選框。因此,在此我通過chk = $('.check input');獲取已檢查的控件。每當用戶檢查支票框中的函數調用。我在這裏設置可見性不是全部文本框和何時用戶單擊它將找到下一個控件,並使用$(this).parent().next().removeClass('hide');刪除類.hde。因此,該複選框旁邊將顯示該文本框。

在你的情況,我認爲在默認情況下你會做文本框disabled=false

.hide is used to set visiblity false.you can do disabling by adding attribute disabled to the textbox

CSS

.hide 
     { 
      display: none; 
     } 

設計器代碼

<ItemTemplate> 
<asp:CheckBox ID="cbEnergyItems" runat="server" CssClass="check" /> 
<input type="text" id="txtQty" style="width: 25px" class="hide" 
    value="0" runat="server" /> 
    <%# Eval("something") %>               
</ItemTemplate> 

jQuery的

$('.check input').bind("click", function() { 
         var chk = $('.check input'); 
         if ($(this).attr('checked')) { 
          //$(this).parent().next().removeClass('hide'); //removing the class to visible. you can do removing the attribute 
           $(this).parent().next().removeAttr("disabled"); 
          if ($(this).parent().next().val() == ""0"") 
           showStatus(true, "Please enter the quantity"); 
         } 
         else { 
           $(this).parent().next("disabled","disabled"). 
          // $(this).parent().next().addClass('hide'); 

         } 

        }); 

我認爲這將解決您的問題。