2017-07-12 41 views
0

我再次打擾你們,我很抱歉。基於Repeater中RadioButtonList的selectedvalue來啓用/禁用一些文本框控件

下面的腳本時RadioButtonList控件是一箇中繼器內,但TextBox控件之外的工作。

<script type="text/javascript"> 
     $(document).ready(function() { 
      $('#rblStatus input').change(function() { 
       if ($(this).val() == "Yes") { 
        $("#txtOnwerName").prop("disabled", true); 
       } 
       else { 
        $("#txtOnwerName").prop("disabled", false); 
       } 
      }); 
     }); 
    </script> 

不過,我面對現在的問題是,無論是RadioButtonList控件和文本框(其中約5)都Repeater控件中。

其結果是,你是否檢查是或在RadioButtonList沒有價值,沒有文本框越來越禁用或啓用。

任何想法我做錯了什麼?

例子:

<form ID="form1" runat="server"> 
<asp:Repeater ID="Repeater1" runat="server"> 
<ItemTemplate> 
    <asp:RadioButtonList ID="rblIssues" runat="server" RepeatDirection="Horizontal" TextAlign="Right"> 
    <asp:ListItem Text="Yes" /> 
    <asp:ListItem Text="No" /> 
    </asp:RadioButtonList><br /> 
    <asp:TextBox ID="txtOnwerName" runat="server"></asp:TextBox> 
</ItemTemplate> 
</asp:Repeater> 
</form> 
+0

ASP.NET不一定會將相同的'id'分配給ASP.NET代碼中定義的HTML控件。您應該使用'ClientID'屬性來爲元素定義HTML'id'。請使用[PasteBin](https://pastebin.com)發佈網頁源代碼(瀏覽器>右鍵單擊>查看源代碼)。 –

回答

0

難道宥檢查生成html?如果是這樣,你會看到控件的ID被重命名。所以你需要通過ClientID或者課程名稱訪問它們。

開始通過包裝在<div>對於初學者一個ItemTemplate的每一個內容。現在的方式是不可能從另一箇中辨別出一個Repeater物品。

<div class="repeaterItem"> 
    <asp:RadioButtonList ID="rblIssues" runat="server" RepeatDirection="Horizontal" TextAlign="Right"> 
     <asp:ListItem Text="Yes" /> 
     <asp:ListItem Text="No" /> 
    </asp:RadioButtonList><br /> 
    <asp:TextBox ID="txtOnwerName" runat="server"></asp:TextBox> 
</div> 

現在您可以根據repeaterItem這個類找到一個項目。檢查值並啓用/禁用文本框。

<script type="text/javascript"> 
    $('.repeaterItem input[type="radio"]').change(function() { 
     if ($(this).val() == "Yes") { 
      $(this).closest('div').children('input[type="text"]').prop("disabled", true); 
     } 
     else { 
      $(this).closest('div').children('input[type="text"]').prop("disabled", false); 
     } 
    }); 
</script> 
相關問題