使用Jquery更新输入值(Updating an input value with Jquery)

编程入门 行业动态 更新时间:2024-10-10 17:29:14
使用Jquery更新输入值(Updating an input value with Jquery)

我有一个包含几个字段的表单,其中一个字段应根据另一个字段的值进行更新。 第一个的值是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()); }); });

更多推荐

本文发布于:2023-07-18 07:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1157047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Jquery   Updating   input

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!