如何在jquery中设置文本框值 [英] how to set textbox value in jquery

查看:218
本文介绍了如何在jquery中设置文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jquery将某个值正确加载到文本框中?尝试下面的一个,但我得到 [object Object] 作为输出。请赐教我,我是jquery的新手。

How do I properly load the a certain value into a textbox using jquery?Tried the one below but I get the [object Object] as output. Please enlighten me on this, I'm new to jquery.

 proc = function(x,y) {
        var str1 = $('#pid').value;
        var str2 = $('#qtytobuy').value;
     var str3= $('#subtotal').load('compz.php?prodid=' + x + '&qbuys=' + y);
    $('#subtotal').val(str3);
    }

这里是html表格:

<form name="yoh" method="get"> 
Product id: <input type="text" name="pid"  value=""><br/>

Quantity to buy:<input type="text" name="qtytobuy"  value="" onkeyup="proc(document.yoh.pid.value, this.value);"></br>

Subtotal:<input type="text" name="subtotal" id="subtotal"  value=""></br>
<div id="compz"></div>

</form>


推荐答案

我想你要设置通话的响应到URL 'compz.php?prodid ='+ x +'& qbuys ='+ y 作为文本框的值对吗?如果是这样,你必须做类似的事情:

I think you want to set the response of the call to the URL 'compz.php?prodid=' + x + '&qbuys=' + y as value of the textbox right? If so, you have to do something like:

$.get('compz.php?prodid=' + x + '&qbuys=' + y, function(data) {
    $('#subtotal').val(data);
});

参考: get()

您的代码中有两个错误:

You have two errors in your code:


  • load() 将从Ajax返回的 HTML 放入指定的元素:


从服务器加载数据并将返回的HTML放入匹配的元素中。

Load data from the server and place the returned HTML into the matched element.

你不能使用该方法设置文本框的值。

You cannot set the value of a textbox with that method.

$(选择器).load()返回一个jQuery 对象。默认情况下,当对象被视为字符串时,对象将转换为 [object Object]

$(selector).load() returns the a jQuery object. By default an object is converted to [object Object] when treated as string.

进一步澄清

假设您的网址返回 5

如果你的HTML看起来像:

If your HTML looks like:

<div id="foo"></div>

然后结果

$('#foo').load('/your/url');

<div id="foo">5</div>

但是在你的代码中,你有一个输入元素。 理论上(它不是有效的HTML,并且无法正常工作),等效的调用会导致

But in your code, you have an input element. Theoretically (it is not valid HTML and does not work as you noticed), an equivalent call would result in

<input id="foo">5</input>

但实际上你需要

<input id="foo" value="5" />

因此,您不能使用 load() 。您必须使用其他方法,获取响应并自行将其设置为值。

Therefore, you cannot use load(). You have to use another method, get the response and set it as value yourself.

这篇关于如何在jquery中设置文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆