开发者

How to Set the Select List & Radio button Values by default

I am using jquery my html code

 <input type="radio" name="maritalstatus" id="maritalstatus" value="single"/>
  single
 <input type="radio" name="maritalstatus" id="maritalstatus" value="married"/>
  Married
 <input type="submit" id="clicksingleact开发者_开发技巧ive" />
 <input type="submit" id="clickmarriedactive"/>

if i submit the click "singleactive" button, the value of the single should checked, in case if i use the "clickmarriedactive" button the value of the married value should checked how to arrive this??

<select id="state" name="state">
 <option value="Ukrain"  >Ukrain</option>
 <option value="U.S"  >USA/option>  
</select>

If i click the "check ukrain button the value should select default in select list, as above.How to achieve in jquery any idea?


First of all, you should change the IDs of your radio buttons. No two elements can have the same ID. (But keep the names same, that's what groups the two options together.)

 <input type="radio" name="maritalstatus" id="maritalstatus1" value="single"/>
  single
 <input type="radio" name="maritalstatus" id="maritalstatus2" value="married"/>
  Married

Then just add click handlers like these:

$('#clicksingleactive').click(function() {
  $('#maritalstatus1').attr('checked', true);
});
$('#clickmarriedactive').click(function() {
  $('#maritalstatus2').attr('checked', true);
});


For 1st one i would prefer giving seperate id to 2 diff options married and single and use its id for selection as following:

<body>
    <input type="radio" name="maritalstatus" id="maritalstatus_1" value="single"/>
      single
     <input type="radio" name="maritalstatus" id="maritalstatus_2" value="married"/>
      Married
 <input type="submit" id="clicksingleactive" />
 <input type="submit" id="clickmarriedactive"/>
<br/>

</body>
<script>
    $(document).ready( function()
    {
        $("#clicksingleactive").click( function()
        {
            $("#maritalstatus_1").attr("checked","checked");
        });
        $("#clickmarriedactive").click( function()
        {
            $("#maritalstatus_2").attr("checked","checked");
        });
    });
</script>


Something like that? (not sure I did fully understand your question)

<html>
<head>
    <title>S.O. 3287253</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        $('#clicksingleactive').click(function() {
            $('#maritalstatus-single').attr('checked', 'checked');
        });
        $('#clickmarriedactive').click(function() {
            $('#maritalstatus-married').attr('checked', 'checked');
        });
        $('#selectUkrain').click(function() {
            $('#state').val('Ukrain');
        });

    });

    </script>
</head>
<body>
<form id="maritalstatus">
    <input type="radio" name="maritalstatus" id="maritalstatus-single" value="single"/> single
    <input type="radio" name="maritalstatus" id="maritalstatus-married" value="married"/> Married
    <select id="state" name="state">
        <option value="" selected="selected">== choose ==</option>
        <option value="Ukrain">Ukrain</option>
        <option value="U.S">USA/option>  
    </select>
    <br/>
    <input type="button" id="clicksingleactive" value="Single"/>
    <input type="button" id="clickmarriedactive" value="Married"/>
    <input type="button" id="selectUkrain" value="Ukrain"/>
</form>
</body>
</html>


SELECT LIST

$("#state").val("Ukrain"); Sets the  Ukrain List
$("#state").val("U.S"); Sets the USA default
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜