简体   繁体   English

onchange将值插入隐藏的输入jquery

[英]onchange insert value into hidden input jquery

When i select some data from my option i need the value to be showed when "onchange" the select... can someone help me ? 当我从我的选项中选择一些数据时,我需要在“onchange”选择时显示的值...有人可以帮我吗?

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

I think you want this as your onchange event: 我想你想要这个作为你的onchange事件:

<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. 当你在没有参数的情况下调用val() ,它会获取元素的值。 If you call it with a parameter like val('some value'); 如果你 val('some value');类的参数调用它val('some value'); , it sets the value of the element. ,它设置元素的值。

If you can, avoid inline event definition on html: 如果可以,请避免在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());
});

要将所选值设置为隐藏控件,您应该:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM