Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

javascript - 如何在jquery中设置文本框值(how to set textbox value in 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.

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

Please enlighten me on this, I'm new to jquery.

(请赐教我,我是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); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <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> 

  ask by Wern Ancheta translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?

(我想你想设置调用URL的反应'compz.php?prodid=' + x + '&qbuys=' + y作为文本框的值对吗?)

If so, you have to do something like:

(如果是这样,你必须做以下事情:)

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

Reference: get()

(参考: get())

You have two errors in your code:

(您的代码中有两个错误:)

  • load() puts the HTML returned from the Ajax into the specified element:

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

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

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

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

    (您无法使用该方法设置文本框的值。)

  • $(selector).load() returns the a jQuery object .

    ($(selector).load()返回一个jQuery 对象 。)

    By default an object is converted to [object Object] when treated as string.

    (默认情况下,对象在被视为字符串时会转换为[object Object] 。)

Further clarification :

(进一步澄清 :)

Assuming your URL returns 5 .

(假设您的网址返回5 。)

If your HTML looks like:

(如果你的HTML看起来像:)

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

then the result of

(然后结果)

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

will be

(将会)

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

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

(从理论上讲 (它不是有效的HTML,并且不像你注意到的那样工作),会导致等效的调用)

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

But you actually need

(但你真的需要)

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

Therefore, you cannot use load() .

(因此,您不能使用load() 。)

You have to use another method, get the response and set it as value yourself.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...