使用jquery从另一个元素获取使用id的输入的id

getting id of a input using id from another element using jquery

本文关键字:id 输入 获取 jquery 另一个 元素 使用      更新时间:2024-01-27

我正在尝试使用jquery获取输入的id。我所做的是获得我单击的按钮的id,然后连接字符串以获得我想要的元素的id。但当我显示var picloc时,它显示未定义。如何获取隐藏输入的id?

$('.photo-frame').click(function (){
var id = this.id;
var picloc = $('#'+id+'_location').val();
$('#picture-selected').html(id);
});
<span class="photo-frame"  id="<?php echo $filetitle2;?>">
</span>
<input id="<?php echo $filetitle2;?>_location" type="hidden">

如果隐藏元素只是DOM中span的下一个,那么您可以使用以下脚本。

$('.photo-frame').click(function (){
var hidden_element = $(this).next('input:hidden').val();
});

试试这个:

$('.photo-frame').click(function (){
var id = $(this).attr('id');
var picloc = $('#'+id+'_location').val();
$('#picture-selected').html(id);
});
var id = this.id;

有一些问题。

this是指文档本身。您应该用$()包围它,以便指向单击的元素。我不知道[object].id是否可用。

按以下方式更改行。

var id = $(this).attr('id');

您确定在$(document).ready(function(){ ... });或页面本身中使用上述代码块吗。

你可以试试这个:

var currentId = $('#element').attr('id');

只有当您有一个有效的jQuery对象$(this)时,这才会起作用,例如:

var currentId = $(this).attr('id');