1

I have a form with a few fields, one of which should be updated based on the value of another. The value of the first is POSTed to another URL, and the returned value should be used to fill the second field. Here's the code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script>        
    function lookup(rid) {
                    $.get("/handler?rid=" + $("input#rid").val(), function(update_rid){
                        $("#name").val(html(update_rid));
                    })
                }
    </script>
        <form name="new_alert">
            <input type="text" name="rid" id="rid" onkeyup="lookup(this.value);">
            <br />
            <input type="text" name="name" id="name">  
        </form>

The POST works fine, and the correct data is returned from /hander, which I confirmed by making a test and filling it using $("#testdiv").html(update_rid);

So it seems like the problem is in the way I'm trying to update the value, but I can't get past that.

1
  • Oops, looks like this was an easy one. Commented Jun 15, 2010 at 7:22

3 Answers 3

4

Remove the html() within val().

$.get("/handler?rid=" + $("input#rid").val(), function(update_rid){
   $("#name").val(update_rid);
});

It still might depend on what kind of data is returned from your server.

note by author

Stay unobtrusive!

Replace your inline onkeyup handler with

$(function(){
    $('#rid').bind('keyup', function(){
        lookup($(this).val());
    });
});
Sign up to request clarification or add additional context in comments.

Comments

3

Unless you defined a function called html somewhere, you should try it without it.

$("#name").val(update_rid);

Comments

0

I suggest input to be,

<input type="text" name="rid" id="rid">

then jQuery as

$('#rid').keyup(function(){
    var self = this
    $.get("/handler?rid=" + self.value , function(update_rid){
          $("#name").val(update_rid);
    });
})

1 Comment

It's impressive how the exact same copied answer can take the checkmark, 5 minutes later :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.