문제

I have web form markup as follows:

<input type="radio" value="0" name="fieldSelection[1]" class="optionA">
<input type="radio" value="0" name="fieldSelection[1]" class="optionB">
<input type="radio" value="0" name="fieldSelection[1]" class="optionC">
<input type="radio" value="0" name="fieldSelection[2]" class="optionA"><!--want to check this one-->
<input type="radio" value="0" name="fieldSelection[2]" class="optionB">
<input type="radio" value="0" name="fieldSelection[2]" class="optionC">
<input type="radio" value="0" name="fieldSelection[3]" class="optionA">
<input type="radio" value="0" name="fieldSelection[3]" class="optionB">
<input type="radio" value="0" name="fieldSelection[3]" class="optionC">

How can I set only one of the boxes to checked -- example, check button optionA for fieldSelection[2]?

Here is what I have, but obviously it checkes all 3 buttons:

$('.optionA').prop('checked', true);
도움이 되었습니까?

해결책

You can use the attribute selector:

$('input.optionA[name="fieldSelection[2]"]').prop('checked', true);

Fiddle

다른 팁



Try something like this:

function setChecked(string fieldSelection, string className)
   {
      if ( $.trim($(this).attr('name')) == $.trim(fieldSelection) )
       {
        $("." + className ).prop('checked', true);
       }
    }

Is this what you are looking for?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top