2016-12-17 21 views
-1

這是我的理解,eval()更可能是最好的選擇。但在我的inserttest.php頁面中,它是html和javascript的混合體。當頁面加載完成並將內容放入#light Div時,只有javascript不起作用。返回可執行的Javascript和帶負載的html內容()

所以我的問題是......下面是最好的方法。我怎麼去編輯它以允許所有內容按原樣返回。

<script type="text/javascript"> 
$(document).ready(function(){ 
$("form#myform").submit(function(event){ 
$('#light').load('inserttest.php'); 
}); 
}); 
</script> 

我在這個線程[Run script tags loaded with ajax

eval($('#container script').html()); 

我相信這將只返回腳本並沒有HTML /看見

更新 - insertest.php的輸出

<form id="myformshareid" method="POST" class="form_statusinput"> 
<input type="hidden" name="toid" id="toid" value="<? echo $user1_id ?>"> 
<input type="hidden" name="streamitem_con" id="streamitem_con" value='<? echo $streamitem_data['streamitem_content'] ?>'/> 
<input type="hidden" name="streamitem_idsharetype" id="streamitem_idsharetype" value="<? echo $noterypes['streamitem_type_id'] ?>"> 
<input type="hidden" name="streamitem_id" id="streamitem_id" value="<? echo $streamitem_data['streamitem_id'] ?>"> 
<div cols="40" rows="5" name="newmsg" id="newmsg" data-ph="Say something <? echo $data['first']; ?>..." contenteditable="true"></div> 
<select id="privacy" name="privacy"> 
<option value="1">Public</option> 
<option value="2">Friends</option> 
</select> 
<select id="comments" name="comments"> 
<option value="1">Comments On</option> 
<option value="2">Comments Off</option> 
</select> 
<input title="Share <? echo $data["fullname"] ?>'s ststus to your wall" style="cursor:pointer;color:#FFF;" type="submit" id="button" value="Share"> 
</form> 

<div id="display"></div> 

<script type="text/javascript"> 
$(function(){ 
$('.pop').unbind().click(function() { 
var streamitem_id = $(this).attr('id'); 
$.ajax({ 
type:"POST", 
url: "sharecollect.php", 
data: { streamitem_id: streamitem_id, }, 
success: function(data) { 
$('.pop').css('overflow-y', 'auto'); 
$('body').css('overflow', 'hidden'); 
$("#sharecontent"+streamitem_id).html(data); 
} 
}); 
return false; 
}); 
}); 
</script> 
+0

您可以使用'。.getScript()'...使用GET HTTP請求從服務器加載JavaScript文件,然後執行它。 –

+0

正如此線程所述? [link] http://stackoverflow.com/questions/14521108/dynamically-load-js-inside-js) –

+0

取決於如何在'inserttest.php'中設置腳本。重要的是要明白'document.ready'已經出現在主頁面中。 'eval()'在html上不起作用 – charlietfl

回答

-1

如果您確實需要評估一個腳本,請不要使用eval,但可以這樣:

void function(global) { 
    'use strict'; 

    var betterEval; 

    betterEval = function(src, context) { 
    return Function.call(null, 'global', '"use strict";' + src)(context || global); 
    }; 

    // usage 
    betterEval('console.log(global);'); 
}(this); 
+0

這與在jQuery load()中運行腳本有什麼關係?評估什麼?看不到這個問題如何回答這個問題 – charlietfl

+0

如何在響應中運行嵌入腳本......然後他可以這樣做:'betterEval('var $ = global。$;'+ $('#container ')。text());';) –

+0

但是響應是html,'load()'會執行腳本標記已經 – charlietfl

相關問題