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

发布于 2024-09-10 06:22:05 字数 690 浏览 4 评论 0原文

我正在使用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 中实现任何想法?

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="clicksingleactive" />
 <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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

血之狂魔 2024-09-17 06:22:05

首先,您应该更改单选按钮的 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);
});

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);
});
悲歌长辞 2024-09-17 06:22:05

对于第一个,我更愿意为已婚和单身的 2 个不同选项提供单独的 id,并使用其 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>

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>
霊感 2024-09-17 06:22:05

类似的事情? (不确定我是否完全理解你的问题)

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

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>
dawn曙光 2024-09-17 06:22:05

选择列表

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

SELECT LIST

$("#state").val("Ukrain"); Sets the  Ukrain List
$("#state").val("U.S"); Sets the USA default
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文