jQuery 设定输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2974104/
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
set value of input
提问by JP Hellemons
Sometimes code says more then words, so the following lines work:
有时代码会说更多的话,所以以下几行工作:
$("#text11").append($(xml).find("address").find("street"));
$("#<%= tbWoonplaats.ClientID %>").val('testing?');
but these do not:
但这些没有:
var street = $(xml).find("address").find("street");
$("#<%= tbAdres.ClientID %>").val(street);
it displays [object object] in the input
now i've tried to replace .val(street);
with .val(new string(street));
but that doesn't work either
它在输入中显示 [object object] 现在我已经尝试替换为.val(street);
,.val(new string(street));
但这也不起作用
appending to a span works but setting with .val()
to input doesn't...
附加到跨度有效但设置.val()
为输入不...
<span id="text11"></span>
EDITthe output of
编辑输出
var street = $(xml).find("address").find("street");
window.alert(street);
is: [object Object]
是:[对象对象]
采纳答案by user113716
Try this:
尝试这个:
var street = $(xml).find("address").find("street").text();
You were getting the node with .find("street")
, but not its content, so you needed .text()
.
您使用 获取节点.find("street")
,但未获取其内容,因此您需要.text()
.
EDIT:
编辑:
You can check to see if a street
node was found using the length
property.
您可以street
使用该length
属性检查是否找到了节点。
var street = $(xml).find("address").find("street");
alert(street.length); // should alert at least 1 if the find was successful
回答by Reigel
try..
尝试..
$("#<%= tbAdres.ClientID %>").val(street.html());
or
或者
$("#<%= tbAdres.ClientID %>").val(street.text());
回答by Ilona
$("#<%= tbAdres.ClientID %>").val(street.text());