javascript 对象 HtmlInputElement 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31312259/
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
Value of the Object HtmlInputElement
提问by Daulet Cheri
In my .html file I have one button and string variable str = "<input type=\"text\" value=\"apple\""
. When i click button i want to alert value of these string . How can i parse string to object and just use something like obj.val()
or obj.value
to get it's value . I've tried $.parseHTML(str)
. it returns me Object HtmlInputElement, but i can't use .val()
or .value
, even .attr('value')
?Jquery is added and no errors . Please help . Best Regards .
在我的 .html 文件中,我有一个按钮和字符串变量str = "<input type=\"text\" value=\"apple\""
。当我单击按钮时,我想提醒这些字符串的值。我如何将字符串解析为对象并仅使用类似obj.val()
或obj.value
获取它的值。我试过$.parseHTML(str)
。它返回我Object HtmlInputElement,但我不能使用.val()
or .value
,甚至.attr('value')
?添加了 Jquery 并且没有错误。请帮忙 。最好的祝福 。
回答by Bhushan Kawadkar
Try this: create jquery object from str
and then call .val()
on that object, see below code
试试这个:从创建 jquery 对象str
,然后调用.val()
该对象,见下面的代码
str = "<input type=\"text\" value=\"apple\">";
var strObj = $(str);
alert(strObj.val());
回答by joe_young
If you put your input in your actual html file, and give it an id (e.g. appleInput):
如果你把你的输入放在你的实际 html 文件中,并给它一个 id(例如appleInput):
<input type="text" value="apple" id="appleInput"> <!-- Your input -->
Then in your JavaScript:
然后在你的 JavaScript 中:
var str = document.getElementById("appleInput").value;
alert(str);