如何设置选择列表&单选按钮默认值

时间:2010-07-20 05:28:58

标签: jquery select radio-button

我正在使用jquery我的HTML代码

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

如果我提交单击“singleactive”按钮,则应检查单个值,如果我使用“clickmarriedactive”按钮,结婚值的值应该检查如何到达这个?

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

   

如果我点击“检查乌克兰按钮,值应该在选择列表中选择默认值,如上所述。如何在jquery中实现任何想法?

4 个答案:

答案 0 :(得分:1)

首先,您应该更改单选按钮的ID。没有两个元素可以具有相同的ID。 (但保持名称相同,即将两个选项组合在一起。)

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

然后只需添加以下点击处理程序:

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

答案 1 :(得分:0)

选择列表

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

答案 2 :(得分:0)

对于第一个我更喜欢给2个差异选项结婚和单独分配并使用其ID进行选择如下:

<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>

答案 3 :(得分:0)

那样的东西? (不确定我是否完全理解了你的问题)

<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>