Javascript jquery获取输入字段的id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14897768/
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 get id of input field
提问by user1652050
i need to get the id of a input that's inside a span of a sharepoint list. it looks like the code below.
我需要获取共享点列表范围内的输入的 ID。它看起来像下面的代码。
<tr id="idEstablishmentRow">
<td class="ms-formbody" vAlign="top">
<span dir="none">
<span style="vertical-align: middle;">
<input type='text' id='hello' />
</span>
</span>
</td>
</tr>
now i want to get the id of the input. i have tried:
现在我想获取输入的 id。我试过了:
var _test = $('#idEstablishmentRow').find('td.ms-formbody');
var _test1 = _test.find('span.style');
var _test2 = _test1.find('input');
var _test3 = _test2.attr('id');
alert(_test3);
alert(_test2);
but it didn't work. could anyone help me ?
但它没有用。有人可以帮我吗?
回答by Denys Séguret
You might want this :
你可能想要这个:
var id = $('#idEstablishmentRow td.ms-formbody span input').attr('id');
But as your HTML doesn't have any element whose id is idEstablishmentRow, it's hard to be sure of what you exactly want.
但是由于您的 HTML 没有任何 id 为 的元素idEstablishmentRow,因此很难确定您到底想要什么。
回答by red_alert
var inputId = $("span input").attr("id");
回答by Hugo Dozois
Your snippet does not work because of this selector : var _test1 = _test.find('span.style');
由于此选择器,您的代码段不起作用: var _test1 = _test.find('span.style');
When doing this, you try to find a span with the css class "style" not the html attribute style. And thus _test1is empty.
执行此操作时,您尝试找到具有 css 类“样式”而不是 html 属性样式的跨度。因而_test1是空的。
You can do the following to fix it : var _test1 = _test.find('span[style]');
您可以执行以下操作来修复它: var _test1 = _test.find('span[style]');
Though its not really a good selector, because it is most likely going to break if you change the css to remove it from inline.
虽然它不是一个很好的选择器,因为如果您更改 css 以将其从内联中删除,它很可能会中断。
You could add a class to the span
<span class="aclass"></span>. And this way you can select it by the following selector :var _test1 = _test.find('span.aclass');You could also select it in other ways like
var _test1 = _test.find('span:nth-child(2)');if it's always the second child.
您可以向 span 添加一个类
<span class="aclass"></span>。这样您就可以通过以下选择器选择它:var _test1 = _test.find('span.aclass');您还可以通过其他方式选择它,例如
var _test1 = _test.find('span:nth-child(2)');它是否始终是第二个孩子。
These are only two of the multiple possible solutions to go with your _test2and _test3
这些只是适合您_test2和您的多种可能解决方案中的两种_test3
- You can also try to select all spans with inputs in it with
var _test1 = _test.find('span input');or_test.find('span').children('input'). Then you can do your_test3.
- 您还可以尝试使用
var _test1 = _test.find('span input');或选择所有包含输入的跨度_test.find('span').children('input')。然后你可以做你的_test3.
Note that you can regroup the selector in 1 line :
请注意,您可以在 1 行中重新组合选择器:
This has the advantage of not having to put a class on the span since you actually not only query for the span but for a span WITH an input in it.
这具有不必在跨度上放置类的优点,因为您实际上不仅查询跨度,还查询其中包含输入的跨度。
$('tr#idEstablishmentRow td.ms-formbody span input');
//or to get the id directly
$('tr#idEstablishmentRow td.ms-formbody span input').attr('id');

