jQuery 通过 ID 获取子值

发布于 2024-10-03 12:14:03 字数 2136 浏览 1 评论 0原文

我有以下 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}">&nbsp;
   <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人的歌 2024-10-10 12:14:03

您可以使用 .closest() 获取输入

然后 .find()< /code>里面的输入,如下所示:

$(".pane .btn-delete").click(function(){
  var x = $(this).closest(".pane").find(".comm_id").val(); 
  //use x
  //animations...
});

请注意将 id 更改为 class 以便对多个窗格有效,您的 应该是这样的:

<input type="hidden" class="comm_id" value="{$comment.id}">

You can get the input by using .closest() to get to the <div class="pane"> then .find() the input inside, like this:

$(".pane .btn-delete").click(function(){
  var x = $(this).closest(".pane").find(".comm_id").val(); 
  //use x
  //animations...
});

Note the change of id to class to be valid for multiple panes, your <input> should look like this:

<input type="hidden" class="comm_id" value="{$comment.id}">
青春如此纠结 2024-10-10 12:14:03
var x = $("#comm_id").val();

编辑:

实际上,你甚至不需要通过 ID 选择它:

var x = $( "input[type=hidden]", $( this ).parents(".pane") ).val();

如果你有几个这样的代码片段,你可以使用它来解决问题。

var x = $("#comm_id").val();

EDIT:

actually, you don't even need to select it by ID: you could just use

var x = $( "input[type=hidden]", $( this ).parents(".pane") ).val();

this would solve the problem if you have several fragments of code like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文