Javascript 如何在jquery中设置文本框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4193916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set textbox value in jquery
提问by Wern Ancheta
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.
如何使用 jquery 将某个值正确加载到文本框中?尝试了下面的一个,但我得到了[object Object]
作为输出。请赐教,我是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>
回答by Felix Kling
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:
我认为您想将对 URL 的调用响应设置'compz.php?prodid=' + x + '&qbuys=' + y
为文本框的值,对吗?如果是这样,您必须执行以下操作:
$.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 HTMLreturned from the Ajax into the specified element: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.
$(selector).load()
returns the a jQuery object. By default an object is converted to[object Object]
when treated as string.
load()
将从 Ajax 返回的HTML放入指定的元素中:从服务器加载数据并将返回的 HTML 放入匹配的元素中。
您不能使用该方法设置文本框的值。
$(selector).load()
返回一个 jQuery对象。默认情况下,对象[object Object]
在被视为字符串时被转换为。
Further clarification:
进一步说明:
Assuming your URL returns 5
.
假设您的 URL 返回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
但是在您的代码中,您有一个 input 元素。从理论上讲(它不是有效的 HTML 并且不能像您注意到的那样工作),等效的调用将导致
<input id="foo">5</input>
But you actually need
但你实际上需要
<input id="foo" value="5" />
Therefore, you cannot use load()
. You have to use another method, get the response and set it as value yourself.
因此,您不能使用load()
. 您必须使用另一种方法,获取响应并将其设置为自己的值。
回答by Mauro
value is javascript to use jQuery language you should use
值是使用 jQuery 语言的 javascript,您应该使用
$('#pid').val()
to get and
得到和
$('#pid').val('value')
to set it.
设置它。
Your second issue.
你的第二个问题。
I never tried the automatic set of html value of load method, sorry. For sure, you can do something like
没试过load方法自动设置html值,抱歉。当然,你可以做类似的事情
$('#subtotal').load( 'compz.php?prodid=' + x + '&qbuys=' + y, function(response){ $('#subtotal').val(response);
});
Test it cause I am handwrite-coding ;)
测试它,因为我是手写编码;)
回答by Oliver M Grech
I would like to point out to you that .val() also works with selects to select the current selected value.
我想向您指出 .val() 也可以与 select 一起使用来选择当前选定的值。
回答by Kamil Kie?czewski
try
尝试
subtotal.value= 5 // some value
proc = async function(x,y) {
let url = "https://server.test-cors.org/server?id=346169&enable=true&status=200&credentials=false&methods=GET&" // some url working in snippet
let r= await(await fetch(url+'&prodid=' + x + '&qbuys=' + y)).json(); // return json-object
console.log(r);
subtotal.value= r.length; // example value from json
}
<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" id="pid" value=""><br/>
Quantity to buy:<input type="text" id="qtytobuy" value="" onkeyup="proc(pid.value, this.value);"></br>
Subtotal:<input type="text" name="subtotal" id="subtotal" value=""></br>
<div id="compz"></div>
</form>