简体   繁体   English

在jQuery的单选列表控件中禁用单选按钮

[英]Disable radio button in Radio List Control in Jquery

I have to disable radio button from the given Radio list control based on the condition. 我必须根据条件从给定的“单选”列表控件中禁用单选按钮。 The Problem is the Radio button List is auto generated by CMS and id is same on all the list item. 问题在于单选按钮列表是由CMS自动生成的,并且所有列表项上的ID都相同。 How i can apply "disable" class based on the condition. 我如何根据条件应用“禁用”类。 Such as if (some value == am) then show first radio button and disable others two. 例如,如果(某个值== am)然后显示第一个单选按钮,而禁用其他两个。

<div class="fieldset">       
 <div class="radio">
            <label >
                <input id="wffmd4800a78bba44322993e9ef2a91e0ea0_Sections_9__Fields_3__Value" name="wffmd4800a78bba44322993e9ef2a91e0ea0.Sections[9].Fields[3].Value" type="radio" value="Morning (8 AM - 1 PM)">
                <span class="disable">Morning (8 AM - 1 PM)</span>
            </label>
        </div>
        <div class="radio">
            <label>
                <input id="wffmd4800a78bba44322993e9ef2a91e0ea0_Sections_9__Fields_3__Value" name="wffmd4800a78bba44322993e9ef2a91e0ea0.Sections[9].Fields[3].Value" type="radio" value=" Afternoon (1 PM - 5 PM)" aria-invalid="false">
                <span> Afternoon (1 PM - 5 PM)</span>
            </label>
        </div>
        <div class="radio">
            <label>
                <input id="wffmd4800a78bba44322993e9ef2a91e0ea0_Sections_9__Fields_3__Value" name="wffmd4800a78bba44322993e9ef2a91e0ea0.Sections[9].Fields[3].Value" type="radio" value=" Evening (5 PM - 9 PM)">
                <span> Evening (5 PM - 9 PM)</span>
            </label>
        </div>
</div>
</div>

Not such a good idea, but you can use jquery to select the radio by the value of the value attribute and disable the relevant radio (using the prop method): 并不是一个好主意,但是您可以使用jquery通过value属性的值选择单选按钮并禁用相关的单选按钮(使用prop方法):

 $('input[type="radio"][value=" Afternoon (1 PM - 5 PM)"]').prop('disabled', true); $('input[type="radio"][value=" Evening (5 PM - 9 PM)"]').prop('disabled', true); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="fieldset"> <div class="radio"> <label > <input id="wffmd4800a78bba44322993e9ef2a91e0ea0_Sections_9__Fields_3__Value" name="wffmd4800a78bba44322993e9ef2a91e0ea0.Sections[9].Fields[3].Value" type="radio" value="Morning (8 AM - 1 PM)"> <span class="disable">Morning (8 AM - 1 PM)</span> </label> </div> <div class="radio"> <label> <input id="wffmd4800a78bba44322993e9ef2a91e0ea0_Sections_9__Fields_3__Value" name="wffmd4800a78bba44322993e9ef2a91e0ea0.Sections[9].Fields[3].Value" type="radio" value=" Afternoon (1 PM - 5 PM)" aria-invalid="false"> <span> Afternoon (1 PM - 5 PM)</span> </label> </div> <div class="radio"> <label> <input id="wffmd4800a78bba44322993e9ef2a91e0ea0_Sections_9__Fields_3__Value" name="wffmd4800a78bba44322993e9ef2a91e0ea0.Sections[9].Fields[3].Value" type="radio" value=" Evening (5 PM - 9 PM)"> <span> Evening (5 PM - 9 PM)</span> </label> </div> </div> 

 $(document).ready(function(){ var testString="Afternoon (1 PM - 5 PM)";//show only this text control and hide other control $.each($('input[type=radio]'),function(i,v){ if($.trim($(v).val())!=testString)// any condtion you can put here { $(v).addClass("disable").next().addClass("disable"); } }); }); 
 .disable { display : none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="fieldset"> <div class="radio"> <label > <input id="wffmd4800a78bba44322993e9ef2a91e0ea0_Sections_9__Fields_3__Value" name="wffmd4800a78bba44322993e9ef2a91e0ea0.Sections[9].Fields[3].Value" type="radio" value="Morning (8 AM - 1 PM)"> <span class="">Morning (8 AM - 1 PM)</span> </label> </div> <div class="radio"> <label> <input id="wffmd4800a78bba44322993e9ef2a91e0ea0_Sections_9__Fields_3__Value" name="wffmd4800a78bba44322993e9ef2a91e0ea0.Sections[9].Fields[3].Value" type="radio" value=" Afternoon (1 PM - 5 PM)" aria-invalid="false"> <span> Afternoon (1 PM - 5 PM)</span> </label> </div> <div class="radio"> <label> <input id="wffmd4800a78bba44322993e9ef2a91e0ea0_Sections_9__Fields_3__Value" name="wffmd4800a78bba44322993e9ef2a91e0ea0.Sections[9].Fields[3].Value" type="radio" value=" Evening (5 PM - 9 PM)"> <span> Evening (5 PM - 9 PM)</span> </label> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM