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

查看:92
本文介绍了如何获得输入字段的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的输入字段:

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

如何获取输入字段的值?

How do I get the value of the input field?

我这样尝试过:

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

结果是尽管输入字段具有值,例如,我得到未定义的警报. 10.

The result is that I get an alert being undefined though the input field has got a value e.g. 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.

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

推荐答案

这是因为您尚未将该html附加到页面上.可以这样做(someElement.append($("<td>...</td>"));),或者专门搜索该部分

That's because you've not appended that html to the page. Either do that (someElement.append($("<td>...</td>"));), or search in that part specifically

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 $("...").

这篇关于如何获得输入字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆