2015-11-05 100 views
1

我想將我從文本框中獲得的值傳遞給iframe的src。我使用下面的代碼從文本框中獲取值,在按鈕上單擊它應該傳遞給iframe src並替換查詢:'*'與傳遞的變量,即*應該被替換。 如何繼續? Foloowing是用HTML代碼的iframe將jquery變量傳遞給iframe src

<iframe src="http://localhost:5601/#/dashboard/New-Dashboard?embed&_a 
=(filters:!(),panels:!((col:1,id:env,row:1,size_x:4,size_y:3,type:visualization) 
,(col:5,id:env-2,row:1,size_x:4,size_y:3,type:visualization),(col:9,id:env-3, 
row:1,size_x:4,size_y:3,type:visualization)),query:(query_string:(analyze_wildcard: 
!t,query:'*')),title:'New%20Dashboard')&_g=(refreshInterval:(display:Off,pause 
:!f,section:0,value:0),time:(from:now%2Fy,mode:quick,to:now%2Fy))" height="600" 
width="800" id="myframe"></iframe> 



<!DOCTYPE html> 
<html> 
<head> 
    <title>jQuery With Example</title> 
    <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
    $(function() { 
     $('.btnGetName').click(function (event) { 
     var name = $('.txtName').val(); 
     alert(name); 
     }); 
    }); 
    </script> 
</head> 
<body> 
    <div> 
    <input type="text" class="txtName" value="hello" id="querypass"/><br /> 
    <button class="btnGetName">Get Name</button> 
    </div> 
</body> 
</html> 

回答

0

試試這個

var src = $('#myframe').attr('src'); 
 
var newSrc = src.replace("query:'*'", "query:'" + name + "'"); 
 
$('#myframe').attr('src', newSrc);

0

你可以試試下面的代碼:

注:假設你是相同的HTML文檔中:

$('.btnGetName').click(function (event) { 
     var name = $('.txtName').val(); 
     alert(name); 
     var iframeObj = $("#myframe"); 
     var srcString = iframeObj.attr("src"); 
     srcString = srcString.replace("query:'*'","query:'"+name+"'"); 
     iframeObj.attr("src",srcString); 
}); 

請確保您不要在文本字段中輸入單引號。