首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用jquery在编辑用例中生成可编辑文本

如何使用jquery在编辑用例中生成可编辑文本
EN

Stack Overflow用户
提问于 2018-07-23 09:43:52
回答 3查看 118关注 0票数 1

我有一张桌子,就像每个桌子都有编辑按钮:

代码语言:javascript
复制
<table class="table-responsive table-striped  col-lg-12 col-md-12 col-sm-12 padding_left_right_none dash_table">
  <tr>
    <td style="width:10px;">&nbsp;</td>
    <td style="width: 130px;">
      <p class="name"><?php echo $aircrews->fname.'&nbsp;'.$aircrews->lname;  ?></p>
      <input class="text" type="text" name="name" value="<?php echo $aircrews->fname.'&nbsp;'.$aircrews->lname;  ?>" style="height: 22px;width: 110px;border: none;">
    </td>
    <td style="width: 40px;">
      <p class="name"><?php echo $aircrews->pay_status1;  ?></p>
      <input type="text" class="text" name="pay1" value="<?php echo $aircrews->pay_status1;  ?>" style="height: 22px;width: 20px;border: none;text-align: center;">
    </td>
    <td style="width: 40px;">
      <p class="name"><?php echo $aircrews->pay_status2;  ?></p>
      <input type="text" class="text" name="pay2" value="<?php echo $aircrews->pay_status2;  ?>" style="height: 22px;width: 20px;border: none;text-align: center;">
    </td>
    <td class="right_set"><button class="edit_btn">Edit / -</button></td>
  </tr>
</table>

我的问题是,当我单击特定的编辑时,它对应的p标记应该是隐藏的,输入type=text应该是可见的,但在我的示例中,通过单击“编辑”按钮,每个p标记都会隐藏并显示所有输入文本,而不是特定的标记。

我的jQuery代码如下所示:

代码语言:javascript
复制
 <script type="text/javascript">
   $(document).ready(function(){
    $('.text').hide();
     $(this).on('click', function(){
        alert('ok');
        $('.name').hide();
        $('.text').show();
     })
   })
   </script>

我是新来的,谁能帮我把这个联系起来吗?提前鸣谢。我只想点击编辑他们的文本框,而不是让cs

我的观点如下:https://screenshots.firefox.com/9zlXPAB2kw8MYxR7/localhost

我希望点击编辑并命名为pay1和pay2应该是文本

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-23 09:51:53

删除.class删除,因为该事件将应用于.hide().show()中的每一个。在本例中,将click事件直接放在所有p上。(可以很容易地根据您的规格调整)。

this放在'show()hide()中而不是您的类中。要显示当前<td>.text类,可以直接使用jQuery .siblings()函数并指定.text同级。

编辑:

因为您希望在单击Edit按钮而不是每个p时更改文本,所以可以将p.edit_btn放在一起,并使用.parent().children()函数上下遍历DOM。

代码语言:javascript
复制
$(document).ready(function() {
  $('.text').hide();
  $('.edit_btn').on('click', function(e) {
    console.log(this);
    $(this).parent().siblings('td').children('p').hide();
    $(this).parent().siblings('td').children('.text').show();
  })
})
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table-responsive table-striped  col-lg-12 col-md-12 col-sm-12 padding_left_right_none dash_table">
  <tr>
    <td style="width:10px;">&nbsp;</td>
    <td style="width: 130px;">
      <p class="name">
Name      </p>
      <input class="text" type="text" name="name" value="<?php echo $aircrews->fname.'&nbsp;'.$aircrews->lname;  ?>" style="height: 22px;width: 110px;border: none;">
    </td>
    <td style="width: 40px;">
      <p class="name">
name      </p>
      <input type="text" class="text" name="pay1" value="<?php echo $aircrews->pay_status1;  ?>" style="height: 22px;width: 20px;border: none;text-align: center;">
    </td>
    <td style="width: 40px;">
      <p class="name">
name      </p>
      <input type="text" class="text" name="pay2" value="<?php echo $aircrews->pay_status2;  ?>" style="height: 22px;width: 20px;border: none;text-align: center;">
    </td>
    <td class="right_set"><button class="edit_btn">Edit / -</button></td>
  </tr>
</table>

票数 1
EN

Stack Overflow用户

发布于 2018-07-23 09:50:00

您正在按类访问元素。$('.name')返回类名的所有元素,$('.text')还返回类文本的所有元素,而不仅仅是单击的元素。因此,您需要给出单个元素唯一的id并按id $('#id')进行选择。请参阅https://api.jquery.com/id-selector/

票数 0
EN

Stack Overflow用户

发布于 2018-07-23 10:08:28

您应该像这样更改Javascript部分:

代码语言:javascript
复制
<script type="text/javascript">
    $(document).ready(function() {
      $('.edit_btn').on('click', function() {
        $(this).parents().siblings().children('p').hide();
      })
    })
</script>

在jQuery中,.parents()函数将检查"edit_btn“类的父级,即<td>。然后,.siblings()函数考虑<td>标记的所有兄弟类,该标记位于相同的<tr>标记下。然后,.children('p')函数将检查带有<p>标记的子程序,这意味着它考虑了<p>标签下的<td>标记,然后hide()函数将隐藏特定的<p>标记。检查下面的片段。

代码语言:javascript
复制
$(document).ready(function() {
  $('.edit_btn').on('click', function() {
    $(this).parents().siblings().children('p').hide();
  })
})
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table-responsive table-striped  col-lg-12 col-md-12 col-sm-12 padding_left_right_none dash_table">
  <tr>
    <td style="width:10px;">&nbsp;</td>
    <td style="width: 130px;">
      <p class="name">Name 1_1</p>
      <input class="text" type="text" name="name" value="Value Here 1_1" style="height: 22px;width: 110px;border: none;">
    </td>
    <td style="width: 130px;">
      <p class="name">Name 2_1</p>
      <input type="text" class="text" name="pay1" value="Value Here 2_1" style="height: 22px;width: 110px;border: none;text-align: center;">
    </td>
    <td style="width: 80px;">
      <p class="name">Name 3_1</p>
      <input type="text" class="text" name="pay2" value="Value Here 3_1" style="height: 22px;width: 110px;border: none;text-align: center;">
    </td>
    <td class="right_set"><button class="edit_btn">Edit / -</button></td>
  </tr>

  <tr>
    <td style="width:10px;">&nbsp;</td>
    <td style="width: 130px;">
      <p class="name">Name 2_1</p>
      <input class="text" type="text" name="name" value="Value Here 2_1" style="height: 22px;width: 110px;border: none;">
    </td>
    <td style="width: 80px;">
      <p class="name">Name 2_2</p>
      <input type="text" class="text" name="pay1" value="Value Here 2_2" style="height: 22px;width: 110px;border: none;text-align: center;">
    </td>
    <td style="width: 80px;">
      <p class="name">Name 2_3</p>
      <input type="text" class="text" name="pay2" value="Value Here 2_3" style="height: 22px;width: 110px;border: none;text-align: center;">
    </td>
    <td class="right_set"><button class="edit_btn">Edit / -</button></td>
  </tr>
</table>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51475812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档