jQuery 通过 ID 获取子值
我有以下 HTML:
<div class="pane">
<h3>
<input type=hidden id="comm_id" value="{$comment.id}">
<label id="comm_name" onclick="this.style.display='none';document.getElementById('comm_name_edit_view').style.display='';document.getElementById('comm_name_edit').value=this.innerHTML;">{$comment.writer_name}</label>
<div id="comm_name_edit_view" style="display:none;">
<input type=text id="comm_name_edit" value="{$comment.writer_name}">
<button onclick="this.parentNode.style.display='none';document.getElementById('comm_name').innerHTML=getElementById('comm_name_edit').value;document.getElementById('comm_name').style.display='';">حفظ</button>
</div>
</h3>
<p>
<label id="comm_content" onclick="this.style.display='none';document.getElementById('comm_content_edit_view').style.display='';document.getElementById('comm_content_edit').value=this.innerHTML;">{$comment.comment}</label>
<div id="comm_content_edit_view" style="display:none;">
<textarea type=text id="comm_content_edit">{$comment.comment}</textarea>
<button onclick="this.parentNode.style.display='none';document.getElementById('comm_content').innerHTML=getElementById('comm_content_edit').value;document.getElementById('comm_content').style.display='';">حفظ</button>
</div>
</p>
<p><a href="#" class="btn-delete">delete</a> | <a href="#" class="btn-approve">approve</a> | <a href="#" class="btn-spam">spam</a></p>
</div>
和以下 jQuery (我不擅长 jQuery)
jQuery(function(){
$(".pane:even").addClass("alt");
$(".pane .btn-delete").click(function(){
var x=SOMETHING;
$(this).parents(".pane").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow")
return false;
}); ..... etc
在 var x=SOMETHING;
中,我希望能够获取 input
的值该窗格中 ID 为 comm_id
的 code> 框。这有可能吗?
谢谢。
I have the following HTML:
<div class="pane">
<h3>
<input type=hidden id="comm_id" value="{$comment.id}">
<label id="comm_name" onclick="this.style.display='none';document.getElementById('comm_name_edit_view').style.display='';document.getElementById('comm_name_edit').value=this.innerHTML;">{$comment.writer_name}</label>
<div id="comm_name_edit_view" style="display:none;">
<input type=text id="comm_name_edit" value="{$comment.writer_name}">
<button onclick="this.parentNode.style.display='none';document.getElementById('comm_name').innerHTML=getElementById('comm_name_edit').value;document.getElementById('comm_name').style.display='';">حفظ</button>
</div>
</h3>
<p>
<label id="comm_content" onclick="this.style.display='none';document.getElementById('comm_content_edit_view').style.display='';document.getElementById('comm_content_edit').value=this.innerHTML;">{$comment.comment}</label>
<div id="comm_content_edit_view" style="display:none;">
<textarea type=text id="comm_content_edit">{$comment.comment}</textarea>
<button onclick="this.parentNode.style.display='none';document.getElementById('comm_content').innerHTML=getElementById('comm_content_edit').value;document.getElementById('comm_content').style.display='';">حفظ</button>
</div>
</p>
<p><a href="#" class="btn-delete">delete</a> | <a href="#" class="btn-approve">approve</a> | <a href="#" class="btn-spam">spam</a></p>
</div>
and the following jQuery (I'm bad at jQuery)
jQuery(function(){
$(".pane:even").addClass("alt");
$(".pane .btn-delete").click(function(){
var x=SOMETHING;
$(this).parents(".pane").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow")
return false;
}); ..... etc
In the var x=SOMETHING;
, I want to be able to get the value of the input
box with ID of comm_id
in that pane. Is it somehow possible?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
.closest()
获取输入然后
.find()< /code>
里面的输入,如下所示:
请注意将
id
更改为class
以便对多个窗格有效,您的应该是这样的:
You can get the input by using
.closest()
to get to the<div class="pane">
then.find()
the input inside, like this:Note the change of
id
toclass
to be valid for multiple panes, your<input>
should look like this:编辑:
实际上,你甚至不需要通过 ID 选择它:
如果你有几个这样的代码片段,你可以使用它来解决问题。
EDIT:
actually, you don't even need to select it by ID: you could just use
this would solve the problem if you have several fragments of code like that.