javascript - 如何获取已删除元素的 ID - jquery UI

标签 javascript jquery jquery-ui

我已经尝试了很多方法来获取 jquery UI 中放置的元素的 id。请帮助获取id的值。

$( function() {    

    $(".draggable").draggable({
        revert: "invalid",      
        helper: "clone"     
    });

$( "#droppable2" ).droppable({
        drop: function( event, ui ) {
            var draggable = ui.draggable;           
            var dragged = draggable.clone(); 
            var currentID = ui.draggable.attr("id");/*draggable.find('id'); - returns an object. but, could not get the id. */

            alert(dragged.html());          
            alert(currentID);
            dragged.resizable();
            dragged.appendTo("#droppable2");

            //alert("open properties");
      }
    });

  } );

被删除元素的 html 返回,它包含 id。

---html---

<div class="ui-widget-content draggable">
                <select id='singleSelect'>
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="opel">Opel</option>
                  <option value="audi">Audi</option>
                </select>
            </div>

<div id='droppable2' class="ui-widget-header" height='100%'><p/></div>

最佳答案

您需要找到 select先得到它的id ,因为 ui.draggable返回一个 nodeList ,正如我在评论中所说,您使用的是 <p>标记错误,更正一点:

$(function() {

  $(".draggable").draggable({
    revert: "invalid",
    helper: "clone"
  });

  $("#droppable2").droppable({
    drop: function(event, ui) {
      var draggable = ui.draggable;
      var dragged = draggable.clone();
      var currentID = ui.draggable.find('select').attr('id');
      console.log(currentID);
      dragged.resizable();
      dragged.appendTo("#droppable2");

      //alert("open properties");
    }
  });

});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

<div class="ui-widget-content draggable">
  <select id='singleSelect'>
                  <option value="volvo">Volvo</option>
                  <option value="saab">Saab</option>
                  <option value="opel">Opel</option>
                  <option value="audi">Audi</option>
                </select>
</div>

<div id='droppable2' class="ui-widget-header" height='100%'>
  <p>some paragraph</p>
</div>

关于javascript - 如何获取已删除元素的 ID - jquery UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42436857/

相关文章:

javascript - AngularJS $apply 范围 var 引发异常

javascript - 在页面上显示 JSON?

javascript - cssRules 无法读取多个样式表类属性值

javascript - 获取图像高度属性并将其显示在 CSS 转换中

javascript - 第二次调用 Function 时,Jquery Autocomplete _renderItem 不起作用

css - 覆盖了 jQuery 按钮的全屏 Canvas

javascript - 显示正在运行的已部署服务

javascript - 将一个数中包含的所有整数相加

"border-radius"值的 Javascript/jQuery 正则表达式

jqueryUI自定义按钮不显示