2

asp.netCheckBoxListコントロールによって生成されたチェックボックスがたくさんあります。ページのコントロールの横にユーザーに表示されるテキストを取得したいと思います。

RadioButtonListのような同様のコントロールを使用して、次のようにすることでjQueryで値を取得できました。

var selected = $("input:checked[id*='" + Control.id + "']");

次に、ループして値を取得します。

var whatIwant = selections[i].value;

(この場合、その「値」は私が望むテキストになります)。

ただし、-CheckBoxListのレンダリングは異なります。ListItemごとに、入力だけでなく、次のようなhtmlタグもあります。

<input id="ctl00_ContentPlaceHolder1_ConsultedList_0" type="checkbox" name="ctl00$ContentPlaceHolder1$ConsultedList$0" />
<label for="ctl00_ContentPlaceHolder1_ConsultedList_0">Other service providers</label>

ご覧のとおり、私の素敵な小さなjQueryクエリが見つける入力タグ自体には、必要な情報「その他のサービスプロバイダー」が含まれていません。それはラベルタグにあります。

Can anyone think of a good solution for this - maybe a good way of making the CheckBoxList render the text I need in the input tag, or some clever jQuery to find the labels that correspond to the selected inputs?

4

4 に答える 4

10

すでに使用しているセレクターを使用できます (ただし、clientId のみを使用します)。次に、次の兄弟 (ラベル) を取得し、そのテキスト値を取得します。

例えば

$("#" + Control.ClientId).next().text();

または、 for 属性を使用してラベルを取得することもできます

$("label[for=" + Control.ClientId + "]").text();
于 2009-08-17T05:51:26.667 に答える
5

フォーム要素を反復処理し、ラベルを検索してその内部テキストを取得する必要があります。ASP .NET は、コントロールの ClientID を使用してテーブル内のチェックボックスをラップします。次のようなセレクターを使用できます。

  var selection = [];
  $('#<%= CheckBoxList.ClientID %> :checked').each(function () {
    selection.push($(this).next('label').text());
  });

ASP .NET によってレンダリングされた CheckBoxLists の HTML は、次のようになります。

<table id="check1" border="0">
    <tr>
        <td><input id="check1_0" type="checkbox" name="check1$0" />
                <label for="check1_0">Item 1</label></td>
    </tr><tr>
        <td><input id="check1_1" type="checkbox" name="check1$1" />
                <label for="check1_1">Item 2</label></td>
    </tr>
  .....
</table>

ここで、ASP .NET によって生成されたマークアップの例を確認できます

于 2009-08-17T06:09:58.547 に答える
1

ListItems のテキストと値が異なるかどうか見てみましょう。-

以下のように、チェックボックスまたはチェックボックスリストの値/テキストを取得する可能性のあるすべてのケースを説明しました:-

ケース 1:- CheckBoxList 項目のテキストと値が同じ場合

<asp:CheckBoxList ID="CheckBoxList1" runat="server" Width="180">
            <asp:ListItem Value="SQL" Text="">SQL</asp:ListItem>
            <asp:ListItem Value="ASP.Net" Text="">ASP.Net</asp:ListItem>
            <asp:ListItem Value="jQuery" Text="">jQuery</asp:ListItem>
            <asp:ListItem Value="MVC" Text="">MVC</asp:ListItem>
            <asp:ListItem Value="IIS" Text="">IIS</asp:ListItem>
        </asp:CheckBoxList>

以下のjs関数をonclickまたは必要な場所で呼び出します(必要な場合はdocument.readyブロックで呼び出します)

function getChkVal1() {
            $chk_values = $("[id$=CheckBoxList1] input[type=checkbox]:checked").map(function (index, foElem) {
                return $(this).next().html();
            }).get();

            alert($chk_values);
        }

ケース 2:- CheckBoxList 項目の値が数値順の場合 (例: 1,2,3 ...n もちろん、この方法を何度も使用します)

<asp:CheckBoxList ID="CheckBoxList2" runat="server" BackColor="#ffcccc" Width="180">
            <asp:ListItem Value="1" Text="">SQL</asp:ListItem>
            <asp:ListItem Value="2" Text="">ASP.Net</asp:ListItem>
            <asp:ListItem Value="3" Text="">jQuery</asp:ListItem>
            <asp:ListItem Value="4" Text="">MVC</asp:ListItem>
            <asp:ListItem Value="5" Text="">IIS</asp:ListItem>
        </asp:CheckBoxList>

js関数は以下のようになります

function getChkVal2() {
            $chk_values = $("[id$=CheckBoxList2] input[type=checkbox]").map(function (index, foElem) {
                if ($(this).is(":checked"))
                    return (index + 1);
            }).get();

            alert($chk_values);
        }

ケース 3:- CheckBoxList アイテムの値がテキストの省略形/イニシャルである場合 (たとえば、Mango の場合は「M」とします)

ここでは、トリックを使用してページ上のチェックボックスの値を取得し、js スクリプトの作成中に簡単にアクセスできるようにしました。つまり、html 解析中にチェックボックス リストで使用可能なすべての値を取得し、その値でページに格納する、チェックボックス リストで hiddenfield を使用しています。以下を参照してください:-

<asp:CheckBoxList ID="CheckBoxList3" runat="server" BackColor="#ffccff" Width="180">
            <asp:ListItem Value="S" Text="">SQL</asp:ListItem>
            <asp:ListItem Value="A" Text="">ASP.Net</asp:ListItem>
            <asp:ListItem Value="J" Text="">jQuery</asp:ListItem>
            <asp:ListItem Value="M" Text="">MVC</asp:ListItem>
            <asp:ListItem Value="I" Text="">IIS</asp:ListItem>
        </asp:CheckBoxList>
        <input type="hidden" id="hdnChkBox3" value='<%=String.Join( ",", CheckBoxList3.Items.Cast<ListItem>().Select(item=>item.Value) ) %>' />

この隠しフィールドを使用して cs にコードを記述する必要がないため、runat=server ではなく html 隠しフィールドを使用しています。ここでの hiddenfield の役割はヘルパーに他なりません。ヘルパーは、js の記述中に快適さを提供し、チェックボックスの値を取得します。

そして今、スクリプトがどのように見えるか見てみましょう:-

function getChkVal3() {
            $chk_items = $("#hdnChkBox3").val().split(","); // Get all checkboclist values from hiddenfield and convert it to array using split()
// so now we have ItemIndex and Values Index in Array as parallel            
$chk_values = $("[id$=CheckBoxList3] input[type=checkbox]").map(function (index, foElem) {
                if ($(this).is(":checked")) {
                    return ($chk_items[index]);     
                }
            }).get();

            alert($chk_values);
        }

同様に、radibuttonlist/radiobutton でも実行できます。

于 2014-07-15T15:39:18.170 に答える
0

使ってみて Control.ClientID

ソース [MSDN]

例えば

 var selected = $("input:checked[id*='" + control.clientid + "']");

その他のオプションについては、ソースを参照してください。

于 2009-08-17T05:50:57.667 に答える