最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onchange insert value into hidden input jquery - Stack Overflow

programmeradmin9浏览0评论

When i select some data from my option i need the value to be showed when "onchange" the select... can someone help me ?

<select name="search_school" id="search_school" onchange="$('#search_school').val() = $('#school_name').val()">

I want the selected option value to be showed in the hidden input

<input type="hidden" name="school_name" id="school_name" value="" />

When i select some data from my option i need the value to be showed when "onchange" the select... can someone help me ?

<select name="search_school" id="search_school" onchange="$('#search_school').val() = $('#school_name').val()">

I want the selected option value to be showed in the hidden input

<input type="hidden" name="school_name" id="school_name" value="" />
Share Improve this question asked Aug 24, 2009 at 18:13 williamwilliam 2172 gold badges3 silver badges7 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 6

I think you want this as your onchange event:

<select name="search_school" id="search_school" onchange="$('#school_name').val($('#search_school').val())">

When you call val() without a parameter, it fetches the value of the element. If you call it with a parameter like val('some value');, it sets the value of the element.

If you can, avoid inline event definition on html:

<select name="search_school" id="search_school">
...
</select>
<input type="hidden" name="school_name" id="school_name" />

$(document).ready(function () {
    $('#search_school').change(function () {
        $('#school_name').val($(this).val());
    });
});

You want something like this, I think:

$("#search_school").change(function(){
  $(this).val($("#search_school option:selected").text());
});

To set the selected value to your hidden control you should :

<select name="search_school" id="search_school"  onchange="$('#school_name').val($('#search_school').val())">
<script type="text/javascript">
    $(document).ready(function() {
        $("#search_school").change(
            function () {
                $('#school_name').attr('value', $("#search_school").val());
            }
        );
    });
</script>
发布评论

评论列表(0)

  1. 暂无评论