2016-10-11 13 views
0

私のリピーターはユーザーコントロール内にあり、asp:repeaterを使用します。リピーターのチェックボックスの一意のIDを取得するにはどうすればよいですか?

<asp:Repeater ID="rptrNotOnFile" Runat="server"> 
     <ItemTemplate>  
      <tr> 
       <td>&nbsp;</td> 
       <td>Item <asp:Label ID="NotOnFileItemNumber" Runat="server" />: 
        <asp:Label ID="lblPartNumberNotOnFile" Runat="server" /> 
       </td> 
       <td> 
        <asp:Checkbox id="chkPartSelected" runat="server" onClick="isSamChecked()"/> 
       </td> 
       <td> 
        <asp:Checkbox id="chkPartWatchParts" runat="server"/> 
       </td>    
       <td> 
        <asp:Checkbox id="samSelected" runat="server" onClick="isMbChecked()"/> 
            </td>    
      </tr> 
     </ItemTemplate>   
    </asp:Repeater> 

チェックボックスchkPartSelectedとsamSelectedは、各リピータ項目であると、私は他にはその逆のチェックされたときに1をオフにしようとしています:ここに私のコードです。私はハードリピーターの各チェックボックスの一意のIDをコーディングが、私は、1つまたは多くのリピーターのアイテムを持つことができるので、私はそれがハードコードされたくない場合

<script> 
function isMbChecked() { 


     if ($('#ResultsNotOnFile_rptrNotOnFile_ctl01_chkPartSelected').is(":checked")) 

      ($('#ResultsNotOnFile_rptrNotOnFile_ctl01_chkPartSelected').prop("checked", false)) 


    } 
    function isSamChecked() { 

       if ($('#ResultsNotOnFile_rptrNotOnFile_ctl01_samSelected').is(":checked")) 

        ($('#ResultsNotOnFile_rptrNotOnFile_ctl01_samSelected').prop("checked", false)) 

    } 
</script> 

これらの機能の作業:

は、ここに私のjqueryの機能です。

この一意のIDを取得して関数に渡すにはどうすればよいですか?私があなただったら任意の助け

おかげ

JGraham

答えて

0

は、私はちょうど彼らが一意ではありませんので、IDを捨て、そしてそれらをより識別できるように、これらのいくつかのクラスを追加します。

あなたはjQueryのを使用している場合

<asp:Checkbox runat="server" class="sam" onClick="isSamChecked()"/> 
<asp:Checkbox runat="server" class="mb" onClick="isMbChecked()"/> 

次にあなたが

<script> 
function isMbChecked() { 
    var closest = $(this).parent('td').siblings().children('.sam'); 
    closest.prop('checked', !closest.prop('checked')); 
} 

function isSamChecked() { 
    var closest = $(this).parent('td').siblings().children('.mb'); 
    closest.prop('checked', !closest.prop('checked')); 
} 
</script> 
+0

返信いただきありがとうございます。私はこの仕事を私のためにすることができませんでした。私はJSとJQに新しいので、何か間違ったことがあるかもしれません。 – JGraham

+0

@Jグラハムちょうど私が作った間違いに気づいた。今すぐチェックしてください。 – Gavin

0

ような何かを行うことができ、その後、onclickの機能は以下のようにjQueryのセレクタに置き換えることができます。最初にすべてのチェックボックスのチェックを外し、jQueryのクリック機能の中でこれを使用してクリックされたチェックボックス(クリックされたチェックボックス要素をポイントする)キーワードのみをチェックします。 リピータが一意のIDを持つ場合は、下のセレクタを使用できます。

$('#rptrNotOnFile Checkbox[id="chkPartSelected"]').click (function() { 

    $('checkbox').attr('checked',false); 

    this.attr('checked',true); 

}) 
0

IDを取得する方法の2つの例を示します。最初は純粋なjavascriptです。 2番目のスニペットはjQueryを使用し、.aspxページでいくつかの変更が必要です。この最初の例が機能するには

<script type="text/javascript"> 
    function isChecked(element) { 
     //set the checkbox names as fixed variables 
     var checkbox1_name = "chkPartSelected"; 
     var checkbox2_name = "samSelected"; 

     //new variables for the id's to be found 
     var checkbox1_id = ""; 
     var checkbox2_id = ""; 

     //split the id of element 
     //looks something like this: mainContentPane_rptrNotOnFile_chkPartWatchParts_0 
     var idArray = element.id.split('_'); 

     //loop all the items in the array to build the id's 
     for (var i = 0; i < (idArray.length - 2) ; i++) { 
      checkbox1_id += idArray[i] + "_"; 
      checkbox2_id += idArray[i] + "_"; 
     } 

     //add the checkbox names and the last item of the array 
     checkbox1_id += checkbox1_name + "_" + idArray[idArray.length - 1]; 
     checkbox2_id += checkbox2_name + "_" + idArray[idArray.length - 1]; 

     //set both checkboxed to unchecked 
     document.getElementById(checkbox1_id).checked = false; 
     document.getElementById(checkbox2_id).checked = false; 

     //check the checkbox that was clicked 
     element.checked = true; 
    } 
</script> 

ちょうど私たちは、チェックボックスのIDを見つけるために、リピータの行番号を使うことになるonClick="isSamChecked()"この第二の例では

onClick="isChecked(this)"に変更。

リピータが手動で追加する必要があるページにRepeaterがそのIDを追加しないため、最初にリピータ<table id="rptrNotOnFile_table">を囲むテーブルにIDを追加します。 テンプレートの<tr rownumber="<%# Container.ItemIndex %>">のTRの属性としての行番号。 チェックボックス自体にイベントはありません<asp:CheckBox ID="chkPartSelected" runat="server" />

そして最後にスクリプト:

<script type="text/javascript"> 
    $(document).ready(function() { 
     //bind a click function to the checkboxes in the table 
     $("#rptrNotOnFile_table input[type='checkbox']").click(function() { 
      //find the row number from the attribute of the above tr 
      var rowNumber = $(this).closest('tr').attr("rowNumber"); 

      //the function bound to the click on the checkboxes 
      isChecked(rowNumber) 

      //check the checkbox that was clicked 
      this.checked = true; 
     }) 
    }); 

    function isChecked(rowNumber) { 
     //set the id of the repeater as fixed variable 
     var repeater_id = "<%= rptrNotOnFile.ClientID %>"; 

     //set the checkbox names as fixed variables 
     var checkbox1_name = "chkPartSelected"; 
     var checkbox2_name = "samSelected"; 

     //new variables for the id's to be found 
     var checkbox1_id = repeater_id + "_" + checkbox1_name + "_" + rowNumber; 
     var checkbox2_id = repeater_id + "_" + checkbox2_name + "_" + rowNumber; 

     //set both checkboxed to unchecked 
     document.getElementById(checkbox1_id).checked = false; 
     document.getElementById(checkbox2_id).checked = false; 
    } 
</script> 

他の2件の回答が示すようにあなたはおそらく、はるかに簡単にこれを行うことができます。しかし、質問はまた、データ(ListView、GridView、Repeaterなど)を繰り返すasp.net要素からIDを取得することだったので、私はこの広範な答えを提供しました。

関連する問題