jQuery在选择单选按钮时更改输入文本

时间:2011-02-24 02:52:12

标签: jquery

尝试在选择单选按钮时更改<input class="box" type="text" value="??" name="searchQuery"的值=“”。因此,如果用户按下“名字”单选按钮,它会将.box输入的值更改为诸如“输入名字”之类的值,并让其为其他单选按钮执行此操作。

有人可以帮忙吗? jQuery新手在这里:)

<!DOCTYPE html>
<html>
<head>

    <link rel="stylesheet" href="global.css"  media="screen" />
    <link rel="stylesheet" href="design.css"  media="screen" />
    <style type="text/css">
    .hidden {
    display: none;
    }
    </style>
    <script type="text/javascript" src="inc/jquery.js"> </script>
    <script type="text/javascript">$(function() {$('input[type=text]').focus(function() {$(this).val('')});});</script>
    <!-- // -->
**<script>
    $("#searchSelect").change(function () {
          var str = "";
          $("radio option:option1").each(function () {
                str += $(this).text() + " ";
              });
          $("div").text(str);
        })
        .change();
</script>**
</head>

<body>
<div id="top_wrapper_bg">
    <div id="wrapper_top">
        <div id="header">
                <div id="logo">
                <a href="index.php"><img src="img/logo-02-01-11.png" alt="logo-02-01-11" width="300" height="100" border="0" /></a>
                </div><!-- /logo -->
                <div id="header_image">
                right
                </div><! -- /header_image -->
                <div id="clear"> </div><! -- /clear -->
                <div id="nav">
                nav
                </div><! -- /nav -->
        </div><!-- /header-->
    </div><!-- /wrapper_top -->
</div><!-- /top_wrapper_bg -->

<div id="header_search_break"> </div>

<div id="bot_wrapper_bg">       
    <div id="wrapper_bottom">
            <div id="search"> 
                    <span class="medfont">Search by:</span> 
                        <form id="searchSelect" action="#">
                            <input type="radio" name="option1" value="First Name" />First Name&nbsp;
                            <input type="radio" name="option1" value="Last Name" />Last Name&nbsp;
                            <input type="radio" name="option1" value="Department" />Department&nbsp;
                            <input type="radio" name="option1" value="Course" />Course
                        </form>
                        <br>            
                    <input class="box" type="text" name="searchQuery" value="Select an option..." class="textField clearMeFocus" />
            </div><!-- /search -->
            <div id="latest_stats">
            <p>Stats</p>
            </div><!-- /latest_stats -->
            <div id="clear"> </div>
        <div id="contain_stats">
                <div id="latest_prof">
                latest prof
                </div><!-- /latest_prof -->

                <div id="top_prof">
                top prof
                </div><!-- /top_prof -->
                <div id="clear"> </div><! -- /clear -->
        </div><!-- /contain_stats -->
        <br><br><br><br><br><br><br><br>

        <h6 style="margin:0; padding:0; font-size:0.3em;"><a href="prof.php">Prof Page</a></h6>

    </div><!-- /wrapper_bottom -->
</div><!-- /bot_wrapper_bg -->
</body>
</html>

5 个答案:

答案 0 :(得分:1)

Really Simple fiddle.

使用代码:

$(document).ready(function(){
  $("input[type=radio]").click(function(){
     $("#it").val(this.value);
  }); 
});

和HTML:

<input type="text" id="it" value="">
<input type="radio" name="hey" value="one">
<input type="radio" name="hey" value="two">
<input type="radio" name="hey" value="three">

这表明了如何做到这一点的概念。

答案 1 :(得分:1)

<script type="text/javascript">
    $(document).ready(function(){
        $('#searchSelect input[type=radio]').change(function(){
            $('input.box').val('Enter ' + $(this).val());
        });

    });
</script>

答案 2 :(得分:0)

为无线电输入提供一类“选项”或称之为

<input type='radio' class='option' name='option' value='First Name'>

然后是jquery:

$('.option').click(function(){
   var thisRadio = $(this).val();
   $('.box').val(thisRadio);
});

答案 3 :(得分:0)

它将绑定表单 searchSelect 内所有输入的事件处理程序更改,名称为 option1 类型为radio

$(function() {
    $('#searchSelect input[name="option1"]:radio').change(function() { 
        $('.box').val('Enter a ' + this.value); 
    });
});

要了解有关更改事件的详情,请查看jQuery documentation

答案 4 :(得分:0)

$(document).ready(function() {
    $("input[name=option1]:radio").click(function(eventObj) {
        $("input[name=searchQuery]").val("Enter a " + $(eventObj.target).val());
    });
});