jQueryを使用してasp:RadioButtonListから選択した値を読み取る
-
08-07-2019 - |
質問
次のようなコードがあります...
<p><label>Do you have buffet facilities?</label>
<asp:RadioButtonList ID="blnBuffetMealFacilities:chk" runat="server">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:RadioButtonList></p>
<div id="HasBuffet">
<p><label>What is the capacity for the buffet?</label>
<asp:RadioButtonList ID="radBuffetCapacity" runat="server">
<asp:ListItem Text="Suitable for upto 30 guests" value="0 to 30"></asp:ListItem>
<asp:ListItem Text="Suitable for upto 50 guests" value="30 to 50"></asp:ListItem>
<asp:ListItem Text="Suitable for upto 75 guests" value="50 to 75"></asp:ListItem>
<asp:ListItem Text="Suitable for upto 100 guests" value="75 to 100"></asp:ListItem>
<asp:ListItem Text="Suitable for upto 150 guests" value="100 to 150"></asp:ListItem>
<asp:ListItem Text="Suitable for upto 250 guests" value="150 to 250"></asp:ListItem>
<asp:ListItem Text="Suitable for upto 400 guests" value="250 to 400"></asp:ListItem>
</asp:RadioButtonList></p>
</div>
ラジオリストblBuffetMealFacilities:chkがクライアント側を変更し、jQueryからHasBuffet divでスライドダウン機能を実行するときにイベントをキャプチャしたい。これを作成する最良の方法は、ページにいくつかの同様のセクションがあることを念頭に置いて、ラジオリストの「はい」と「いいえ」の回答に応じて質問を表示することです。
解決
this:
$('#rblDiv input').click(function(){
alert($('#rblDiv input').index(this));
});
クリックされたラジオボタンのインデックスを取得します(テストされていないと思います)(#rblDivでRBLをラップする必要があることに注意してください
これを使用して、次のように対応するdivを表示できます。
$('.divCollection div:eq(' + $('#rblDiv input').index(this) +')').show();
それはあなたの意図ですか?
編集:別のアプローチは、rblにクラス名を付けてから行くことです:
$('.rblClass').val();
他のヒント
RadioButtonList1のチェック済み値を取得する簡単な方法は次のとおりです。
$('#RadioButtonList1 input:checked').val()
ティムによる編集:
where RadioButtonList1
はRadioButtonListのClientIDでなければなりません
var rblSelectedValue = $("#<%= RadioButtonList1.ClientID %> input:checked");
これは私のために働いた...
<asp:RadioButtonList runat="server" ID="Intent">
<asp:ListItem Value="Confirm">Yes!</asp:ListItem>
<asp:ListItem Value="Postpone">Not now</asp:ListItem>
<asp:ListItem Value="Decline">Never!</asp:ListItem>
</asp:RadioButtonList>
ハンドラー:
$(document).ready(function () {
$("#<%=Intent.ClientID%>").change(function(){
var rbvalue = $("input[name='<%=Intent.UniqueID%>']:radio:checked").val();
if(rbvalue == "Confirm"){
alert("Woohoo, Thanks!");
} else if (rbvalue == "Postpone"){
alert("Well, I really hope it's soon");
} else if (rbvalue == "Decline"){
alert("Shucks!");
} else {
alert("How'd you get here? Who sent you?");
}
});
});
重要な部分: $(<!> quot; input [name = '<!> lt;%= Intent.UniqueID%<!> gt;']:radio:checked <!> quot;)。val();
私によると、問題なく動作します...
これで試してください
var GetValue=$('#radiobuttonListId').find(":checked").val();
GetValue(Variable)に保存されるRadiobuttonlist値。
ラジオボタンの代わりにラジオボタンリストは、一意のidタグname_0、name_1などを作成します。選択されるテストを簡単に行う方法は、次のようなcssクラスを割り当てることです
var deliveryService;
$('.deliveryservice input').each(function () {
if (this.checked) {
deliveryService = this.value
}
これを試してください:
$("input[name='<%=RadioButtonList1.UniqueID %>']:checked").val()
以下は、最終的に作成したコードです。最初に簡単な説明。 <!> quot; q _ <!> quot;を使用しました。ラジオボタンの質問リストを囲むdiv名の場合。その後、<!> quot; s _ <!> quot;すべてのセクション。次のコードは、チェックされた値を見つけるために質問をループし、関連するセクションでスライドアクションを実行します。
var shows_6 = function() {
var selected = $("#q_7 input:radio:checked").val();
if (selected == 'Groom') {
$("#s_6").slideDown();
} else {
$("#s_6").slideUp();
}
};
$('#q_7 input').ready(shows_6);
var shows_7 = function() {
var selected = $("#q_7 input:radio:checked").val();
if (selected == 'Bride') {
$("#s_7").slideDown();
} else {
$("#s_7").slideUp();
}
};
$('#q_7 input').ready(shows_7);
$(document).ready(function() {
$('#q_7 input:radio').click(shows_6);
$('#q_7 input:radio').click(shows_7);
});
<div id="q_7" class='question '><label>Who are you?</label>
<p>
<label for="ctl00_ctl00_ContentMainPane_Body_ctl00_ctl00_chk_0">Bride</label>
<input id="ctl00_ctl00_ContentMainPane_Body_ctl00_ctl00_chk_0" type="radio" name="ctl00$ctl00$ContentMainPane$Body$ctl00$ctl00$chk" value="Bride" />
</p>
<p>
<label for="ctl00_ctl00_ContentMainPane_Body_ctl00_ctl00_chk_1">Groom</label>
<input id="ctl00_ctl00_ContentMainPane_Body_ctl00_ctl00_chk_1" type="radio" name="ctl00$ctl00$ContentMainPane$Body$ctl00$ctl00$chk" value="Groom" />
</p>
</div>
以下により、質問を必須にすることができます...
<script type="text/javascript">
var mandatory_q_7 = function() {
var selected = $("#q_7 input:radio:checked").val();
if (selected != '') {
$("#q_7").removeClass('error');
}
};
$(document).ready(function() {
$('#q_7 input:radio').click(function(){mandatory_q_7();});
});
</script>
実際の表示/非表示レイヤーの例
<div class="section" id="s_6">
<h2>Attire</h2>
...
</div>
以下のコードを試してください:
<script type="text/javascript">
$(document).ready(function () {
$(".ratingButtons").buttonset();
});
</script>
<asp:RadioButtonList ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server"
AutoPostBack="True" DataSourceID="SqlDataSourceSizes" DataTextField="ProdSize"
CssClass="ratingButtons" DataValueField="_ProdSizeID" Font-Size="X-Small"
ForeColor="#666666">
</asp:RadioButtonList>
Andrew Bullockソリューションは正常に機能します。私に見せて警告を追加したかっただけです。
// Works素晴らしい
$('#<%= radBuffetCapacity.ClientID %> input').click(function (e) {
var val = $('#<%= radBuffetCapacity.ClientID %>').find('input:checked').val();
//Do whatever
});
$('#<%= radBuffetCapacity.ClientID %>').change(function (e) {
var val = $('#<%= radBuffetCapacity.ClientID %>').find('input:checked').val();
//Do whatever
});
値を取得するためにVinhの答えに投票しました。
対応するラベルを見つける必要がある場合は、次のコードを使用できます:
$('#ClientID' + ' input:checked').parent().find('label').text()
私のシナリオでは、JSは別のファイルにあったため、ClientID応答出力を使用することは役に立ちませんでした。驚いたことに、このソリューションはCssClassをRadioButtonListに追加するのと同じくらい簡単でした。これは DevCurry
ソリューションが消えた場合に備えて、ラジオボタンリストにクラスを追加します
<asp:RadioButtonList id="rbl" runat="server" class="tbl">...
記事が指摘しているように、ラジオボタンリストがレンダリングされると、クラス<!> quot; tbl <!> quot;周囲のテーブルに追加されます
<table id="rbl" class="tbl" border="0">
<tr>...
CSSクラスが追加されたため、cssクラスセレクターに基づいて、テーブル内のinput:radioアイテムを参照できます
$(function () {
var $radBtn = $("table.tbl input:radio");
$radBtn.click(function () {
var $radChecked = $(':radio:checked');
alert($radChecked.val());
});
});
繰り返しますが、これは<!> quot; ClientID <!> quot;の使用を避けます。上記で述べたように、私のシナリオでは面倒です。これがお役に立てば幸いです!
なぜそんなに複雑なのですか
$('#id:checked').val();
問題なく動作します!
簡単な解決策が見つかりました。これを試してください:
var Ocasiao = "";
$('#ctl00_rdlOcasioesMarcas input').each(function() { if (this.checked) { Ocasiao = this.value } });