2014-02-07 86 views
0

當我使用welcome.jsp運行時,dojo動態地創建了一個文本框。但是當我在index.jsp中調用welcome.jsp時,它只顯示了div element.which並未在該div內創建文本字段.. .........請有人幫助我? 聽到的是代碼。動態dojo textfield創建div與ajax?

      index.jsp 



<!DOCTYPE html> 
<%@page import="java.util.*" %> <%@page import="org.hibernate.*,org.hibernate.cfg.*,example.*" %> 
<html> 
<head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.7/dojo/dojo.js" data-dojo-config="parseOnLoad: true, isDebug: true"></script> 

    <link rel="stylesheet" type="text/css" href="dojo-release-1.9.2-src/dijit/themes/claro/claro.css"> 
    <link rel="stylesheet" type="text/css" href="dojo-release-1.9.2-src/dojo/resources/dojo.css"> 
    <link rel="stylesheet" type="text/css" href="dojo-release-1.9.2-src/dijit/themes/tundra/tundra.css"> 
<script> 
require(["dojox/layout/ContentPane"]); 
function showCustomer(str) 
{ 
var xmlhttp;  
if (str=="") 
    { 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
    } 
    } 
    alert("str is"+str); 
xmlhttp.open("GET","welcome.jsp",true); 
xmlhttp.send(); 
alert(4); 
} 
</script> 
</head> 
<body class="tundra"> 

<form action=""> 
<select name="customers" onchange="showCustomer(this.value)"> 
<option value="">Select a customer:</option> 


<option value="customer1">customer1</option>  

</select> 
</form> 
<br> 
<div data-dojo-type="dojox/layout/ContentPane" id="txtHint" data-dojo-props="href: '/welcome.jsp', executeScripts: true"> 
    Customer info will be listed here... 
</div> 

</body> 
</html> 

        welcome.jsp 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<%@page import="java.util.*" %> <%@page import="org.hibernate.*,org.hibernate.cfg.*,example.*" %> 
<html> 
<head> 
    <title>Dojo DnD test</title> 


    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.7/dojo/dojo.js" data-dojo-config="parseOnLoad: true, isDebug: true"></script> 

    <link rel="stylesheet" type="text/css" href="dojo-release-1.9.2-src/dijit/themes/claro/claro.css"> 
    <link rel="stylesheet" type="text/css" href="dojo-release-1.9.2-src/dijit/themes/claro/document.css"> 
    <link rel="stylesheet" type="text/css" href="dojo-release-1.9.2-src/dijit/themes/claro/dijitTests.css"> 
    <body onload="vali();"> 
    </body> 
<script> 
dojo.require("dojo.dnd.Source"); 
dojo.require("dijit.form.TextBox"); 
dojo.require("dijit.form.Button"); 
require(["dojox/layout/ContentPane"]); 
var widgets = new Array(); 
var source; 
var c=new Array(); 


dojo.ready(function() { 




for (var i = 0; i < 5; i++) { 
    widgets.push(
    new dijit.form.TextBox({ 
     value: i, 
     class: 'dojoDndItem', 
    }).placeAt("source")); 
} 

source = new dojo.dnd.Source("source", { 
    copyOnly: false 
}); 
new dojo.dnd.Source("target"); 
}); 
setValue = function() { 
    widgets[0].attr("value", (new Date).getTime()); 
}; 

insertNew = function() { 
widgets.push(
    new dijit.form.TextBox({ 
     value: widgets.length, 
     class: 'dojoDndItem', 
    })); 

    source.insertNodes(true, [widgets[widgets.length-1].domNode]); 
}; 
</script> 

</head> 
<body class="claro"> 
<form name="form1"> 
    <div dojoType="dijit.form.Button" onClick="setValue"> 
    set value textbox 0 
</div> 
<div dojoType="dijit.form.Button" onClick="insertNew"> 
    insert new textbox 
</div> 
<table> 
<tr> 
<td> 
<div id="source" style="height: 200px; width: 200px; border: 1px solid red;"> 

</div> </td> 
<td><div id="target" style="height: 200px; width: 200px; border: 1px solid blue;"> 

</div></td></tr></table> 

</form> 
</body> 
</html> 

回答

0

這是因爲當您使用AJAX加載內容時,腳本不會被解釋。您必須使用eval()函數自己評估它們。或者,由於您使用的是Dojo,因此您可以查看dojox/layout/ContentPane模塊。該模塊允許您通過AJAX請求加載頁面並執行其上的腳本,而不需要整個開銷(並且不必手動調用eval())。

爲了與dojox/layout/ContentPane做到這一點,你只用ID #txtHint取代你<div>

<div data-dojo-type="dojox/layout/ContentPane" id="txtHint" data-dojo-props="href: 'welcome.jsp', executeScripts: true"> 
    Customer info will be listed here... 
</div> 

你只需要確保您加載道場,使parseOnLoad: true屬性設置設置data-dojo-config屬性和你需要確保您具有下列代碼導入dojox/layout/ContentPane模塊:

require(["dojox/layout/ContentPane", "dojo/parser"]); 

完整的例子加載網頁可以在找到(我將href更改爲/,因此它會再次加載主頁,因爲我沒有welcome.jsp頁面)。

另一個以編程方式執行相同操作的示例可以在此JSFiddle上找到。

+0

你能告訴我如何將dojox/layout/ContentPane模塊添加到我的代碼中嗎? – user3282682

+0

我使用'dojox/layout/ContentPane'模塊更新了我的答案。我也注意到你以前的評論,回答這個問題:用'eval()'做起來並不容易。您必須解析響應字符串並獲取'