jQuery从隐藏字段中获取值onclick并将值添加到另一个文本字段

时间:2014-05-05 12:00:55

标签: jquery html onclick

HTML:

From: <input id="from" name="from" type="text" class="form-control input-md">
To: <input id="to" name="to" type="text" class="form-control input-md">

<input type="text" name="fromHidden" value="">
<input type="text" name="toHidden" value="">

jQuery的:

<script type="text/javascript"> 

    $(document).ready(function() {

        $('#from').click(function() {
            var input = $( '#from input').val();
            $("#fromHidden").attr("value", input);
        });

        $('#to').click(function() {
            var input = $( "#to").val();
            $("#fromTo").val(input);
        });

    }); 

</script>

我想从HTML输入字段'to''from'获取值,并将其添加到隐藏文本字段'toHidden''fromHidden'。这是在'to'和'from'改变的任何时候完成的。

我写的jQuery目前没有用。

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:2)

您可以使用attribute selector for name获取指定的代码。

$('#from,#to').change(function () {
    $("[name=fromHidden]").val($('#from').val());
    $("[name=toHidden]").val($('#to').val());

});

答案 1 :(得分:0)

  $(document).ready(function() {

        $('#from').blur(function() {
            var input = $( '#from').val();
            $("input[name=fromHidden]").val(input);
        });

        $('#to').blur(function() {
            var input = $( "#to").val();
            $("input[name=toHidden]").val(input);
        });

    }); 

答案 2 :(得分:0)

如果您想更新更改后的值,请使用以下脚本

$('.form-control').on('input propertychanged', function () {
    var input = $(this).val();
    $('[name=' + this.id + 'Hidden]').val(input);
});