简体   繁体   English

如何获得输入字段的值?

[英]How do I get the value of the input field?

this is my input field: 这是我的输入字段:

$("<td class='testclass'><input id='field-search' value='' type='text'></td>")

How do I get the value of the input field? 如何获得输入字段的值?

I tried it this way: 我这样尝试过:

var mytest =$('#field-search').val();
alert(mytest);

The result is that I get an alert being undefined though the input field has got a value eg 10. 结果是,尽管输入字段具有值(例如10),但我得到的警报未定义。

Is there a way to get the value when the value of the input field changes? 输入字段的值更改时,是否有一种获取值的方法?

A link or an example would be very helpful. 链接或示例将非常有帮助。 Thank you. 谢谢。

Link to the code: http://www.file-upload.net/download-2902420/test.user.js.html 链接到代码: http : //www.file-upload.net/download-2902420/test.user.js.html

That's because you've not appended that html to the page. 那是因为您没有将该html附加到页面上。 Either do that ( someElement.append($("<td>...</td>")); ), or search in that part specifically 要么做( someElement.append($("<td>...</td>")); ),要么专门搜索该部分

var e = $("<td class='testclass'><input id='field-search' value='1' type='text'></td>");
var mytest =e.find('#field-search').val();
alert(mytest);

You don't insert element into the page just by calling $("...") . 您不只是通过调用$("...")将元素插入页面。

In jQuery 在jQuery中

var myVal = $('#field-search').val();

In vanilla JavaScript 在香草JavaScript中

var myVal = document.getElementById('field-search').value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM