允许在不清除先前数据的情况下将文本拖放到html输入字段中

时间:2019-04-21 13:36:41

标签: javascript

我正在处理需要输入字段的Django应用程序。用户可以将文本拖放到输入字段中。但是它将清除先前输入的数据。我希望在输入字段中输入所有可拖动的文本。我该如何实现?

<script type="text/javascript" >
{
  function allowDrop(ev) {
    ev.preventDefault();
  }

  function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
  }

  function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    ev.target.value = $("#" + data).html();
  }

}

1 个答案:

答案 0 :(得分:1)

您不能将数据追加到输入字段,但是可以采用旧值并将其添加。获取被eventFilter拖动的值,然后将其添加为旧值,如

import sys
from PyQt5.QtCore import QEvent, QObject, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        loadUi("mainwindow.ui", self)

        self.pushButton_left.installEventFilter(self)
        self.pushButton_middle.installEventFilter(self)
        self.pushButton_right.installEventFilter(self)


    def eventFilter(self, obj, event):
        if event.type() == QEvent.MouseButtonDblClick:
            if event.button() == Qt.LeftButton and obj.objectName() == "pushButton_middle":
                return True
            elif event.button() == Qt.LeftButton and obj.objectName() == "pushButton_right":    
                return True
        if event.type() == QEvent.MouseButtonPress:
            if event.button() == Qt.LeftButton and obj.objectName() == "pushButton_left":
                print("Left click")
            elif event.button() == Qt.LeftButton and obj.objectName() == "pushButton_middle":
                obj.setDown(False)
                return True
            elif event.button() == Qt.MiddleButton and obj.objectName() == "pushButton_middle":
                obj.setDown(True)
                print("Middle click")
            elif event.button() == Qt.LeftButton and obj.objectName() == "pushButton_right":            
                obj.setDown(False)
                return True
            elif event.button() == Qt.RightButton and obj.objectName() == "pushButton_right":
                obj.setDown(True)
                print("Right click")
        elif event.type() == QEvent.MouseButtonRelease:
            if event.button() == Qt.LeftButton and obj.objectName() == "pushButton_left":
                print("Left click")
            elif event.button() == Qt.LeftButton and obj.objectName() == "pushButton_middle":

                return True
            elif event.button() == Qt.MiddleButton and obj.objectName() == "pushButton_middle":
                obj.setDown(False)
                print("Middle click")
            elif event.button() == Qt.LeftButton and obj.objectName() == "pushButton_right":            

                return True
            elif event.button() == Qt.RightButton and obj.objectName() == "pushButton_right":
                obj.setDown(False)
                print("Right click")
        return QObject.event(obj, event)


def main():
    app = QApplication(sys.argv)
    main_window = MainWindow()

    main_window.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
this.slides.forEach((item) => {
  console.log(item.elm.offsetHeight);
})


尝试将文本值拖到输入上
另外,不使用jquery。