2017-10-13 1 views
1

컨트롤러에서 데이터를 가져 와서 Jquery 대화 상자에 표시하는 ajax 메서드가 있습니다. 내 목표는 마우스 대신 데이터를 강조 표시하여 사용자가 데이터를 강조 표시하고 복사하는 대화 상자 내에서 버튼을 사용하는 것입니다.ajax 성공 결과를 clipbaord에 복사하는 방법

아약스

function GrabLink(surveyName) { 
    $.ajax({ 
     type: "GET", 
     url: "/Survey/sendLink", 
     data: { test: surveyName }, 
     contentType: "application/json; charset=utf-8", 
     success: function (data) { 

      $('#my-dialog').html(data); 
      $("#my-dialog").dialog("open"); 

      //alert(data); 
      //$("#my-dialog").show(data); 
     } 
    }) 
} 

JQuery와 대화

$('#my-dialog').dialog({ 
    autoOpen: false, 
    width: 400, 
    resizable: false, 
    modal: true, 
    buttons: { 
     'Copy': function() 
     { 
      //window.prompt("Copy to clipboard: Ctrl+C, Enter", text); 
      // $(this).dialog('close'); 

     } 

    } 
}); 
+0

은 [클립 보드에서보세요. js] (https://clipboardjs.com/). –

+0

내 솔루션 답변으로 문제가 해결 되었습니까? –

+1

예 올바른 답으로 표시했습니다. – cedPound

답변

1

당신은 javascript으로 클립 보드에 복사에 대한 execCommand를 사용할 수 있습니다. 시간적 입력을 만들고 내부에 데이터를 입력 및 제거 :

function clipboard(){ 
    var mydata = document.createElement("input"); 
    document.body.appendChild(mydata); 
    mydata.setAttribute("id", "mydata_id"); 
    document.getElementById("mydata_id").value=Yourdata-success-response; 
    mydata.select(); 
    document.execCommand("copy"); 
    document.body.removeChild(mydata); 
} 
-1

텍스트의 선택없이 복사하는 데이터 코드 아래에 시도/데이터

<head> 
 
    <script type="text/javascript"> 
 
     function CopyToClipboard() { 
 
      var input = document.getElementById ("toClipboard"); 
 
      var textToClipboard = input.value; 
 
      
 
      var success = true; 
 
      if (window.clipboardData) { // Internet Explorer 
 
       window.clipboardData.setData ("Text", textToClipboard); 
 
      } 
 
      else { 
 
        // create a temporary element for the execCommand method 
 
       var forExecElement = CreateElementForExecCommand (textToClipboard); 
 

 
         /* Select the contents of the element 
 
          (the execCommand for 'copy' method works on the selection) */ 
 
       SelectContent (forExecElement); 
 

 
       var supported = true; 
 

 
        // UniversalXPConnect privilege is required for clipboard access in Firefox 
 
       try { 
 
        if (window.netscape && netscape.security) { 
 
         netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect"); 
 
        } 
 

 
         // Copy the selected content to the clipboard 
 
         // Works in Firefox and in Safari before version 5 
 
        success = document.execCommand ("copy", false, null); 
 
       } 
 
       catch (e) { 
 
        success = false; 
 
       } 
 
       
 
        // remove the temporary element 
 
       document.body.removeChild (forExecElement); 
 
      } 
 

 
      if (success) { 
 
       alert ("The text is on the clipboard, try to paste it!"); 
 
      } 
 
      else { 
 
       alert ("Your browser doesn't allow clipboard access!"); 
 
      } 
 
     } 
 

 
     function CreateElementForExecCommand (textToClipboard) { 
 
      var forExecElement = document.createElement ("div"); 
 
       // place outside the visible area 
 
      forExecElement.style.position = "absolute"; 
 
      forExecElement.style.left = "-10000px"; 
 
      forExecElement.style.top = "-10000px"; 
 
       // write the necessary text into the element and append to the document 
 
      forExecElement.textContent = textToClipboard; 
 
      document.body.appendChild (forExecElement); 
 
       // the contentEditable mode is necessary for the execCommand method in Firefox 
 
      forExecElement.contentEditable = true; 
 

 
      return forExecElement; 
 
     } 
 

 
     function SelectContent (element) { 
 
       // first create a range 
 
      var rangeToSelect = document.createRange(); 
 
      rangeToSelect.selectNodeContents (element); 
 

 
       // select the contents 
 
      var selection = window.getSelection(); 
 
      selection.removeAllRanges(); 
 
      selection.addRange (rangeToSelect); 
 
     } 
 
    </script> 
 
</head> 
 
<body> 
 
    <input id="toClipboard" value="text to clipboard"/> 
 
    <button onclick='CopyToClipboard()'>Copy text to clipboard</button> 
 
</body