jquery 中的当前对象值

发布于 2024-08-26 05:33:24 字数 2047 浏览 2 评论 0原文

所有,

在下面的代码中如何编辑当前添加的值。即,在编辑功能中如何获取 td 中的值。还有我们如何检查重复项。

  <script type="text/javascript">
  function edit(obj)
  {
     //   var ele= obj.parentNode ;
        alert(obj.innerHTML);
     var htm = '<textarea name="modal" id="modal" rows="10" cols="10" >'+ele.html+'</textarea>';
     $dialog.html(htm)
           .dialog({
              autoOpen: true,
              position: 'center' ,
              title: 'EDIT',
              draggable: false,
              width : 300,
              height : 400,
              resizable : false,
              modal : true,
              buttons: { "Save" : function() { if($('#modal').val() != ''){ obj.innerHTML=$('#modal').val() ;$dialog.dialog('close');} else {alert('i++'+$('#modal').val()+'++');} } }
           });
     $dialog.dialog('open');
  }


  $(document).ready(function() {
        var flag=0;

              if (flag == 0)
              {
                 var html = '<input type="text" name="val" id="val" />&nbsp;&nbsp;&nbsp;<input type="button" value="Add" id="add"><br>';
                 $('#useroptions').append(html);
                 flag=1;

                 $('#add').click(function(){
                    var a = $('#val').val();
                    if (a == '')
                    {
                       alert('Enter options');
                    }
                    else
                    {
                       var section= '<tr class="clickable"><td id="userval" BGCOLOR="#FF6699">' + a + '&nbsp;&nbsp;&nbsp; <IMG SRC="edit.gif" onclick="javascript:edit(this);" >&nbsp;&nbsp;&nbsp;<IMG SRC="close.gif"
  onclick="javascript:del(this);" ></td></tr>';
                       $('#useroptions').append(section);
                    }
                      }
  }
  });
  });
  </script>

<table id="template">
<tr><td>
<div id="useroptions"></div>
</tr></td>

</table>

All,

in the below code How to edit the current added value.i.e, in the edit function how to get the value in td .Also how can we check for duplicates.

  <script type="text/javascript">
  function edit(obj)
  {
     //   var ele= obj.parentNode ;
        alert(obj.innerHTML);
     var htm = '<textarea name="modal" id="modal" rows="10" cols="10" >'+ele.html+'</textarea>';
     $dialog.html(htm)
           .dialog({
              autoOpen: true,
              position: 'center' ,
              title: 'EDIT',
              draggable: false,
              width : 300,
              height : 400,
              resizable : false,
              modal : true,
              buttons: { "Save" : function() { if($('#modal').val() != ''){ obj.innerHTML=$('#modal').val() ;$dialog.dialog('close');} else {alert('i++'+$('#modal').val()+'++');} } }
           });
     $dialog.dialog('open');
  }


  $(document).ready(function() {
        var flag=0;

              if (flag == 0)
              {
                 var html = '<input type="text" name="val" id="val" />   <input type="button" value="Add" id="add"><br>';
                 $('#useroptions').append(html);
                 flag=1;

                 $('#add').click(function(){
                    var a = $('#val').val();
                    if (a == '')
                    {
                       alert('Enter options');
                    }
                    else
                    {
                       var section= '<tr class="clickable"><td id="userval" BGCOLOR="#FF6699">' + a + '    <IMG SRC="edit.gif" onclick="javascript:edit(this);" >   <IMG SRC="close.gif"
  onclick="javascript:del(this);" ></td></tr>';
                       $('#useroptions').append(section);
                    }
                      }
  }
  });
  });
  </script>

<table id="template">
<tr><td>
<div id="useroptions"></div>
</tr></td>

</table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤城病女 2024-09-02 05:33:24

首先:您将笨重的内联 onXXX 处理程序与更优雅的 jQuery 事件处理程序混合在一起。为什么?

至于您的“如何获取 td 中的值”问题:您的 edit(obj) 将获取作为 obj 参数传递的图像,因为 onclick 位于 img 元素上。您可以像这样找到容器tdvar td = $(obj).closest('td')。然后获取它的 .text() 或任何你想要的。

但同样:您可以使用 实时绑定 做得更好。

First off: you're mixing clunky inline onXXX handlers with more elegant jQuery event handlers. Why?

As for your "how to get the value in td" question: your edit(obj) will get the image passed as as the obj parameter, since the onclick is on the img element. You can find the container td like this: var td = $(obj).closest('td'). Then get its .text() or whatever you want.

But again: you could do this better using live binding.

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