tags:

views:

28

answers:

3

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"&gt;&lt;/script&gt;
    <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  A: 

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

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

Jacob Relkin
+1  A: 

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());
    });
});
jAndy
A: 

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);
    });
})
Reigel
@jAndy - winks... no hard feelings...
Reigel
It's impressive how the exact same copied answer can take the checkmark, 5 minutes later :p
jAndy
it was not copied... sigh...
Reigel