开发者

How to assign dropdown selected value to a hidden field

i have a dropdownli开发者_StackOverflow中文版st control in asp.net which resides in a user control .The yuser control has a updatepannel .I want to set the dropdown selected value to a hidden field .How will i do this in javascript /Jquery .I don't want to use server code ?


With jQuery it's pretty straightforward:

$('#select_id').bind('change', function(){
   $('#hiddenfield_id').val($(this).val());
});

See it in action: http://www.jsfiddle.net/YjC6y/15/

(you would have to change the type from "text" to "hidden")


Pretty much the same as jAndy, but adds the server tags to get the ID for the ASP.NET Controls.

<script type="text/javascript">
$('#<%= ddlDropDownList.ClientID %>').change(function() {
  $('#<%= htxtHiddenField.ClientID %>').val($(this).val());
});
</script>


$('#hiddenFieldID').val( $('#selectMenyID').val() );

Or if you want to do it whenever the user changes the menu, do it using a .change() handler:

$(function() {
    $('#selectMenyID').change(function() {
        $('#hiddenFieldID').val( $(this).val() );
    });
});

You get and set the value of a form element with .val(). When you don't give it an argument, you're getting the value. When you do, you're setting the value.

So a more verbose version of the same thing could look like this:

$(function() {
    $('#selectMenyID').change(function() {
            // Get the value, and store it in a variable
        var theValue = $(this).val();
            // Set that value to the hidden input
        $('#hiddenFieldID').val( theValue );
    });
});

Wrapping your code with $(function() {...}); is a shortcut for calling jQuery's .ready() method, which makes sure the elements are loaded before the code runs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜