我有一个包含几个字段的表单,其中一个字段应根据另一个字段的值进行更新。 第一个的值是POST到另一个URL,返回的值应该用于填充第二个字段。 这是代码:
<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>POST工作正常,从/ hander返回正确的数据,我通过测试并使用$(“#testdiv”)填充它来确认.html(update_rid);
因此,问题似乎是我试图更新价值的方式,但我无法超越它。
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.
最满意答案
删除val()的html() val() 。
$.get("/handler?rid=" + $("input#rid").val(), function(update_rid){ $("#name").val(update_rid); });它仍然可能取决于从您的服务器返回什么类型的数据。
作者注意
保持不引人注目!
用你的inline onkeyup handler替换
$(function(){ $('#rid').bind('keyup', function(){ lookup($(this).val()); }); });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()); }); });更多推荐
发布评论