0

Here is jquery script used to select radio button.

function radio(){
       var presetValue = "scholar";
$("[name=identity]").filter("[value="+presetValue+"]").attr("checked","checked");
}

The scrip is invoked on page load. This is XHTML peace of code:

<label for="scholar"> Scholar </label>
        <input type="radio" name="identity" id="scholar" value="scholar"  />
        </td>

        <td>
        <label for="oracle"> Oracle </label>
        <input type="radio" name="identity" id="oracle" value="oracle"/>
        </td>

        <td>
        <label for="both"> Both </label>
        <input type="radio" name="identity" id="both" value="both"/>

When the program runs that 'scholar' radio button is not selected. What might be the problem? Best regards

ucas
  • 417
  • 1
  • 11
  • 30

3 Answers3

1

If you're using jQuery >= 1.6, you should use prop().

Also, you're using a complicated selector for nothing. Since your inputs have id's, this will do it:

$('#scholar').prop('checked', true);

If presetValue changes (i.e: you simplified the code), do this:

$('#' + presetValue).prop('checked', true);
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
0

Check out this answer: how to set radio option checked onload with jQuery

Community
  • 1
  • 1
Dan Smith
  • 5,685
  • 32
  • 33
0
function radio(){
       var presetValue = "scholar";
$("input[value="+presetValue+"]").attr("checked",true);
}
Liam Bailey
  • 5,879
  • 3
  • 34
  • 46