javascript jQuery 获取文本框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22052348/
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
jQuery to get value of textbox
提问by gemini6609
I need to write some jQuery/javascript to do some validation before submitting a form, because I have to check per some very specific results. Per my tests I believe I am accessing the correct row of data as $(this) in my table with my validation tests, however I cannot get the value typed into the text box of the second td of the row. Something is incorrect with:
在提交表单之前,我需要编写一些 jQuery/javascript 来进行一些验证,因为我必须检查一些非常具体的结果。根据我的测试,我相信我通过验证测试在我的表中访问了正确的数据行 $(this),但是我无法将值输入到该行的第二个 td 的文本框中。有什么不正确的:
alert($(this).closest('td').find($("input[name=test]")).val())
alert($(this).closest('td').find($("input[name=test]")).val())
which is preventing the jQuery from getting the value typed into the test textbox of the html below.
这阻止了 jQuery 将值输入到下面 html 的测试文本框中。
<tr>
<td>
<select id="selectId3" name="selectId3" data-val-required="The selectId3 field is required." data-val-number="The field selectId3 must be a number." data-val="true">
<option value="">-- Select Area --</option>
<option value="2">2</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="10">10</option>
<option value="13">13</option>
<option value="16">16</option>
<option value="17">17</option>
</select>
</td>
<td>
<span style="label2">
<input id="test" type="text" value="" name="test">
</span>
</td>
<td> </td>
$('#myForm').submit(function () {
$("select").each(function () {
if (($(this).val() != '') && ($(this).val() != '-- Select Area --')) {
alert($(this).closest('td').find($("input[name=test]")).val());
}
});
return false;
});
If anyone can describe the correct jQuery to get the textbox value I would greatly appreciate it.
如果有人可以描述正确的 jQuery 来获取文本框值,我将不胜感激。
Thanks in advance.
提前致谢。
采纳答案by talemyn
closest()
travels up the DOM tree, not down, so if $(this)
equals the current row, you will never find the td
tags inside it using closest('td')
.
closest()
沿着 DOM 树向上移动,而不是向下移动,因此如果$(this)
等于当前行,您将永远不会td
使用closest('td')
.
Assuming that you can't simply use the id
attribute to access the input
directly, as Jamie suggested, you would need to use:
假设您不能简单地使用id
属性input
直接访问,正如杰米建议的那样,您需要使用:
$(this).children("td:eq(0)");
. . . to get the first td
of the row, assuming that $(this)
references the row that you want. To access the value of the select inside it, use:
. . . 获取td
行的第一行,假设$(this)
引用了您想要的行。要访问其中的 select 值,请使用:
$(this).children("td:eq(0)").find("select").val();
EDIT:Actually, given your code, there are a whol bunch of ways that you could access the value of that select
element, but it's hard to tell which would be the most efficient without seeing the rest of the code, to know the context of the logic.
编辑:实际上,鉴于您的代码,您可以通过多种方式访问该select
元素的值,但是在不查看其余代码的情况下,很难判断哪种方式最有效,以了解该元素的上下文逻辑。
UPDATE:Based on your comments below, try this for your code:
更新:根据您在下面的评论,为您的代码试试这个:
$(this).closest("tr").children("td:eq(0)").find("select").val();
. . . or . . .
. . . 或者 。. .
$(this).closest("tr").children("td:eq(0) select").val();
(the first one is faster)
(第一个更快)
回答by Jamie Pollard
Access it by id?
通过 id 访问它?
$("#test").val();
The # in this example means find an element by its id (jQuery docs)
本例中的 # 表示通过 id 查找元素(jQuery 文档)
回答by Mir Gulam Sarwar
var textboxvalue= $(this).closest('td:eq(1) input').val();
eq is 0 based, if 1 for then second td,for textbox we use input
eq 是基于 0 的,如果 1 表示第二个 td,对于我们使用的文本框input