jquery如何获取span元素内的文本框值
我想使用 jquery 获取 span 元素内的文本字段(只有一个)的值,但不能。
<span>simple span text
<a class="remove" href="javascript:" title="Remove">x</a>
<input type="hidden" name="word[ids][]" value="this is text">
</span>
与锚标记删除类关联的 Jquery 代码
$(".remove").live("click", function(){
alert ($(this).parent().('input:text').val()); // return undefined
//i just coded below to check whether span itself is getting or not
alert ($(this).parent().attr('tag')); // this also return undefined
if ($(this).parent().is('span'));
alert("parent is span"); // only this works
});
I want to get the value of textfield(only one) inside span element using jquery but couldn't.
<span>simple span text
<a class="remove" href="javascript:" title="Remove">x</a>
<input type="hidden" name="word[ids][]" value="this is text">
</span>
Jquery code associated to remove class of anchor tag
$(".remove").live("click", function(){
alert ($(this).parent().('input:text').val()); // return undefined
//i just coded below to check whether span itself is getting or not
alert ($(this).parent().attr('tag')); // this also return undefined
if ($(this).parent().is('span'));
alert("parent is span"); // only this works
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果
input
始终是remove
元素的下一个同级元素,那么这将起作用:代码不起作用的解释:
这一行
$(this) .parent().('input:text').val()
在语法和逻辑上都是错误的。如果你想在另一个元素中查找一个元素,你必须使用
find
[docs]:这仍然不会给您值,因为您有一个隐藏
input
元素。如果您查看:text
[docs ] 文档,您将看到该选择器因此不会选择那些。因此,要么只使用'input'
或使用:hidden
[文档]:'input:hidden'
。.attr('tag')
根本不起作用,因为 HTML 元素没有属性 tag。您可以使用nodeName
属性从 DOM 元素获取标签名称:If the
input
is always the next sibling of theremove
element, then this will work:Explanation of why your code does not work:
This line
$(this).parent().('input:text').val()
is syntactically and logically wrong.If you want to find an element inside another one, you have to use
find
[docs]:This will still not give you the value, because you have a hidden
input
element. If you have a look at the:text
[docs] documentation, you will see that this selector thus not select those. So either just use'input'
or use:hidden
[docs]:'input:hidden'
..attr('tag')
simply does not work because an HTML element has no attribute tag. You can get the tag name from the the DOM element using thenodeName
property:这样做:
在这里测试 http://jsfiddle.net/zhiyelee/29bgX/
do like this:
Test it here http://jsfiddle.net/zhiyelee/29bgX/
试试这个:
Try this:
这是行不通的:
相反,您需要使用
find
函数。另一个问题是,您正在寻找文本输入,但您的输入是隐藏类型:在单独的注释中,您的最终警报将始终被警报,无论是父级是否为
span
,因为if
语句末尾有一个分号。This will not work:
Instead, you would need to use the
find
function. Another problem is the fact that you are looking for a text input, but your input is of typehidden
:On a separate note, your final
alert
will always be alerted, whether the parent is aspan
or not, because you have a semi-colon at the end of yourif
statement.这样:
这会找到里面最接近的
span
前行者,这样对 HTML 代码的修改会更加灵活。希望这有帮助。干杯
With this:
This will find inside the closest
span
antecessor, so will be more flexible to HTML code changes.Hope this helps. Cheers