我在创建表单时遇到问题:在javascript中选择我的spring mvc应用程序。
function createSubSelect(divName,pathName,defaultValue,defaultLable,items){
var newDiv=document.createElement('div');
var selectHTML = "";
selectHTML="<form:select path=";
selectHTML += "'"+pathName+"'";
selectHTML += "class=";
selectHTML += "'aa'>";
selectHTML += "<form:option value=";
selectHTML += " '"+defaultValue+"' ";
selectHTML += "label=";
selectHTML += "'"+defaultLable+"'";
selectHTML += ">";
selectHTML += "<form:options items=";
selectHTML += "'"+items+"'";
selectHTML += "</form:options>";
selectHTML += "</form:select>";
selectHTML += "<form:errors path=";
selectHTML += "'"+pathName+"'";
selectHTML += "/>";
alert(selectHTML);
document.getElementById(divName).appendChild(newDiv);
}
请在我错误的地方帮助我。
答案 0 :(得分:0)
首先,您似乎正在使用JavaScript构建JSP字符串。这不起作用,因为JSP在服务器上进行评估,服务器将HTML发送到浏览器。 JavaScript需要处理HTML和文档对象模型API。
执行此操作的基本方法是:
var newDiv=document.createElement('div');
var selectHTML = [
'<select name="' + pathName + '" class="aa">',
'<option value="' + defaultValue + '">' + defaultLable + '</option>',
'<option value="X">Label 1</option>',
'<option value="Y">Label 2</option>',
'</select>'
].join("");
// Inject new HTML into new DIV
newDiv.innerHTML = selectHTML;
// Insert new DIV into page
document.getElementById(divName).appendChild(newDiv);
现在,你有一个名为items
的JavaScript变量,我不知道它包含什么。我假设它是某种阵列。您需要为<option value="foo">Bar</option>
数组中应显示在下拉列表中的每个值创建一个items
,其中foo
是选择该选项时发送回服务器的值,以及{ {1}}是用户可见的标签。