سؤال

لدي التعليمات البرمجية مشابهة لما يلي...

<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 التغييرات من جانب العميل وأداء شريحة أسفل الدالة على HasBuffet شعبة من مسج.ما هي أفضل طريقة لإنشاء هذا ، وإذ تضع في اعتبارها أن هناك عدة أقسام مماثلة على الصفحة ، حيث أريد أسئلة كشف اعتمادا على نعم لا جواب في راديو قائمة.

هل كانت مفيدة؟

المحلول

وهذا:

$('#rblDiv input').click(function(){
    alert($('#rblDiv input').index(this));
});

وسوف تحصل على مؤشر زر الاختيار الذي تم النقر عليه (على ما أظن، لم تختبر) (ملاحظة كنت قد التفاف RBL بك في #rblDiv

هل يمكن بعد ذلك استخدام ذلك لعرض شعبة المقابلة مثل هذا:

$('.divCollection div:eq(' + $('#rblDiv input').index(this) +')').show();

هل هذا ما كنت تعني؟

وتحرير: هناك نهج آخر يتمثل في إعطاء ربل اسم فئة، ثم انتقل:

$('.rblClass').val();

نصائح أخرى

وطريقة بسيطة لاسترداد قيمة التحقق من RadioButtonList1 هي:

$('#RadioButtonList1 input:checked').val()

وتعديل بواسطة تيم:

وحيث RadioButtonList1 يجب أن يكون ClientID من RadioButtonList

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?");
   }
  });
});

الجزء المهم:$("input[name='<%=القصد.UniqueID%>']:راديو:فحص").val();

ووفقا للي انها سوف تعمل بشكل جيد ...

ومجرد محاولة من هذا

   var GetValue=$('#radiobuttonListId').find(":checked").val();

<اقتباس فقرة>   

وقيمة Radiobuttonlist ليتم تخزينها على GetValue (متغير).

وA راديو قائمة زر بدلا من زر الراديو يخلق فريدة من نوعها بطاقات هوية NAME_0، NAME_1 الخ طريقة سهلة لاختبار يتم تحديد وعن طريق تعيين فئة المغلق مثل

var deliveryService;
$('.deliveryservice input').each(function () {
    if (this.checked) {
        deliveryService = this.value
    }

وجرب هذا:

$("input[name='<%=RadioButtonList1.UniqueID %>']:checked").val()

وفيما يلي رمز أنشأنا في نهاية المطاف. A breif التفسير الأول. استخدمنا "q_" للحصول على اسم شعبة ملفوفة حول قائمة الأسئلة زر الراديو. ثم كان لدينا "S_" لأي أقسام. التعليمة البرمجية التالية حلقات عبر الأسئلة للعثور على قيمة التحقق منها، ومن ثم يقوم بعمل الشريحة على القسم المختص.

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>

وأندرو بولوك حل يعمل على ما يرام، وأنا أردت فقط أن تظهر لك الألغام وإضافة تحذيرا.

و// وتعمل كبرى

$('#<%= radBuffetCapacity.ClientID %> input').click(function (e) {
   var val = $('#<%= radBuffetCapacity.ClientID %>').find('input:checked').val();
   //Do whatever
});

و// تحذير - يعمل في فايرفوكس ولكن ليس IE8 .. استخدم هذا لبعض الوقت قبل أن يلاحظ أنه لم يعمل في IE8 ... تستخدم لعمل كل شيء في جميع المتصفحات مع مسج عند العمل في واحدة

$('#<%= radBuffetCapacity.ClientID %>').change(function (e) {
   var val = $('#<%= radBuffetCapacity.ClientID %>').find('input:checked').val();
   //Do whatever
});

ولقد صوت للإجابة فينه للحصول على القيمة.

إذا كنت بحاجة إلى العثور على التسمية المقابلة، يمكنك استخدام هذا الرمز:

و$('#ClientID' + ' input:checked').parent().find('label').text()

عن السيناريو بلدي, بلدي شبيبة كان في ملف منفصل ، وذلك باستخدام ClientID الإنتاج استجابة لم تكن مواتية.من المستغرب أن الحل هو بسيطة مثل إضافة CssClass إلى RadioButtonList التي اكتشفت على DevCurry

مجرد طارئ أن الحل يختفي ، إضافة فئة إلى الراديو الخاص بك زر قائمة

<asp:RadioButtonList id="rbl" runat="server" class="tbl">...

كما يشير المقال ، عندما راديو زر القائمة المقدمة ، فئة "tbl" هو إلحاق المحيطة الجدول

<table id="rbl" class="tbl" border="0">
<tr>...

الآن بسبب فئة CSS التي تم إلحاق يمكنك فقط الرجوع إلى الإدخال:راديو العناصر في الجدول الخاص بك ، استنادا إلى فئة css محدد

$(function () {
            var $radBtn = $("table.tbl input:radio");
            $radBtn.click(function () {
                var $radChecked = $(':radio:checked');
                alert($radChecked.val());
            });
        });

مرة أخرى, هذا يتجنب استخدام "ClientID" المذكورة أعلاه والتي وجدت الفوضى بلدي السيناريو.ويساعد هذا الأمل!

وماذا مجمع ذلك؟

$('#id:checked').val();

وسوف تعمل على ما يرام!

ولقد وجدت حلا بسيطا، حاول هذا:

var Ocasiao = "";    
$('#ctl00_rdlOcasioesMarcas input').each(function() { if (this.checked) { Ocasiao = this.value } });
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top