开发者

jQuery - How to write text values to a hidden field for later use?

开发者 https://www.devze.com 2023-04-05 22:22 出处:网络
I am using the latest and greatest jQuery. I have a form with three text fields and three hidden fields. When you type in a text field, the value will be passed into the respective hidden field, whic

I am using the latest and greatest jQuery.

I have a form with three text fields and three hidden fields. When you type in a text field, the value will be passed into the respective hidden field, which will later be accessed by other means.

FirstName value should be passed to FirstNameTemp
LastName value should be passed to LastNameTemp
Pseudonym value should be passed to PseudonymTemp

The following code is not working for me, though I swear it was working yesterday. I am using FireFox and Firebug. I can watch the value of FirstNameTemp change, which seems to work fine in the code, but nothing else works property.

Is there anything wrong with this? Am I missing something obvious?

<input type='hidden' value="" id="FirstNameTemp">
<input typ开发者_开发问答e='hidden' value="" id="LastNameTemp">
<input type='hidden' value="" id="PseudonymTemp">

<input type='text' value="" class="Search" id="FirstName"><br>
<input type='text' value="" class="Search" id="LastName"><br>
<input type='text' value="" class="Search" id="Pseudonym"><br>


<script type="text/javascript">
$(document).ready(function() {

$(".Search").keyup(function() {
    var FirstName = $("#FirstName").val();
    var LastName = $("#LastName").val();
    var Pseudonym = $("#Pseudonym").val();
    // COPY ENTITIES TO TEMP ENTITIES
    $("#FirstNameTemp").val(FirstName);
    $("#LastNameTemp").val(LastName);
    $("#PseudonymTemp").val(Pseudonym);
});

});

EDIT

I opened and closed my browser and rebooted my machine. Nothing worked. So, I downloaded Firefox and FireBug and reinstalled. It worked just fine.

Thanks to all of you for the help and good ideas. You guys ROCK!!!


Why not do it like this:

$(".Search").keyup(function() {

    $('#'+this.id+'Temp').val($(this).val());

});

Fiddle: http://jsfiddle.net/each/mtFt6/


try this..

$(document).ready(function() {

    $(".Search").keyup(function() {
       var targetId = "#" + $(this).attr("id") + "Temp";
       $(targetId).val($(this).val());
    });

});
0

精彩评论

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