作为这个问题的扩展,我正在尝试将 Javascript 插入到 a<h:commandButton />
的onclick
属性中,因为action
它已经呈现了一个 ajax 表。
我想要做什么:在列表框中获取所选项目并将它们转换为要在 JSF 中使用的参数FileServlet
。IEpara2=value1¶m=value2¶m=value3
这是我所拥有的:
<script type ="text/javascript">
function myScript() {
var box = document.getElementbyId('myForm:box');
var length = box.options.length;
var paramstring = "";
for (var i = 0; i < length; i++) {
if (i != (length - 1) {
if (box.options[i].selected) {
paramstring = paramstring + "param=" + box.options[i].value + "&";
}
} else {
paramstring = paramstring + "param=" + box.options[i].value;
}
}
if (document.getElementById('myForm:checkbox').checked) {
window.location='fileServlet? + paramstring;
}
}
</script>
页面加载时我得到了什么:
javax.servlet.ServletException: Error Parsing /page.xhtml: Error Traced[line:15] The content of elements must consist of well-formed character data or markup.
什么不会触发异常:
<script type ="text/javascript">
function myScript() {
var box = document.getElementbyId('myForm:box');
var length = box.options.length;
var paramstring = "";
if (document.getElementById('myForm:checkbox').checked) {
window.location='fileServlet? + paramstring;
}
}
</script>
一旦我添加,for (var i = 0; i < length; i++)
甚至for (var i = 0; i < 10; i++)
页面都不会加载。为什么它不喜欢 for 循环?