当其中一种发生onchange时,以两种形式发布参数 [英] Post parameters in two forms when onchange happens in one of them
问题描述
我在一个.jsp文件中有两种形式的下拉列表.我希望更改任何列表,以使用两种形式的当前选定参数触发回发到.jsp本身.但是我无法正常工作.这是代码的相关部分:
I have dropdown lists in two forms in a single .jsp. I would like the change of any of the lists to trigger a post back to the .jsp itself with the currently selected parameters in both forms. But I couldn't get it work. Here is the relevant part of the code:
这两种形式都在SearchBrowse.jsp中.这是form1(form2将具有相同的结构.请注意,我尝试使用相同的form id并希望达到这种效果,但没有用):
Both forms are in the SearchBrowse.jsp. This is form1 (form2 will have an identical structure. Note that, I tried to use the same form id and hope to achieve this effect but didn't work):
<c:set var="counter1" value="0"></c:set>
<c:set var="curSp" value="0"></c:set>
<form id="myForm" method="post" action="SearchBrowse.jsp">
<b>Select Species:</b>
<select name="spChoice" size="1" onchange="submit()">
<c:forEach var="sp" items="${species}">
<c:choose>
<c:when test="${param.spChoice == sp.name}">
<c:set var="spFlag" value=" selected"></c:set>
<c:set var="curSp" value="${counter1}"></c:set>
</c:when>
<c:otherwise>
<c:set var="spFlag" value=""></c:set>
</c:otherwise>
</c:choose>
<option value="<c:out value='${sp.name}' />" <c:out value='${spFlag}' />>
<c:out value="${sp.name}"></c:out>
</option>
<c:set var="counter1" value="${counter1 +1}"></c:set>
</c:forEach>
</select>
<br></br>
</form>
这是form2:
<c:set var="counter2" value="0"></c:set>
<c:set var="curChrm" value="0"></c:set>
<%-- Implement a dropdown list and and determine which javabean list to be displayed in the table --%>
<form id="myForm" method="post" action="SearchBrowse.jsp">
<b>Select Chromosome to browse summary:</b>
<select name="chrmChoice" size="1" onchange="submit()">
<c:forEach var="chrms" items="${riceChrmLocation}">
<c:choose>
<c:when test="${param.chrmChoice == chrms.name}">
<c:set var="selectFlag" value=" selected"></c:set>
<c:set var="curChrm" value="${counter2}"></c:set>
</c:when>
<c:otherwise>
<c:set var="selectFlag" value=""></c:set>
</c:otherwise>
</c:choose>
<option value="<c:out value='${chrms.name}' />" <c:out value='${selectFlag}' />>
<c:out value="${chrms.name}"></c:out>
</option>
<c:set var="counter2" value="${counter2 +1}"></c:set>
</c:forEach>
</select>
<br></br>
</form>
当前,当一个表单更改时,另一个下拉列表的选择参数不会回发.我不确定范围是否有问题.我尝试了各种方法,但无法正确完成.我在这里做错了吗?这段代码也有点混乱吗? (如果是,那么您是否有更好的建议以整洁的方式对其进行编码?)非常感谢.
Currently when one form changed, the selection parameter of the other dropdown list is not posted back. I am not sure if it is the problem with scope. I tried various ways but couldn't get it right. Did I do something wrong here? Also is this code a bit messy? (If it is, do u have any better suggestion on coding it in a neat way?) Thanks a lot.
推荐答案
提交表单时,将仅在请求中发送该表单中的字段.
When you submit a form, only the fields from that form are sent on the request.
您可以只使用一种形式(包含所有字段),也可以使用JavaScript在提交之前将值从一种形式复制到另一种形式的隐藏元素中.
You can either have just one form (with all your fields) or use JavaScript before submit to copy values from one form to hidden elements in the other.
这是一个小JS示例:
here is a little JS example:
<!-- test.html -->
<html>
<head>
<script type="text/javascript">
function doCopyAndThenSubmit() {
var sourceInput = document.getElementById("source");
//destination should be the hidden field, made it text to have a visual on the operation
var destinationInput = document.getElementById("destination");
destinationInput.value = sourceInput.value;
//watch the address bar after the OK
alert("Did the copy, press OK for the submit");
document.forms["yourForm"].submit();
}
</script>
</head>
<body>
Add some text in source and change the value in the select<br/>
<form action="test.html" method="GET" name="yourForm">
<select onchange="doCopyAndThenSubmit()">
<option value="x">some value</option>
<option value="y">some other</option>
</select>
<br/>Source:
<!-- id must be unique in the entire document (don't confound with name) -->
<input name="src" id="source" type="text" value="" />
<br/>Destination:
<input name="dest" id="destination" type="text" value="" />
</form>
</body>
</html>
通常,您将拥有具有相同值的name和id属性,以便于跟踪(而不是先按id然后按名称引用一次输入);我使用了不同的值来加强差异.当然,您将以一种形式出现源,而以另一种形式出现目的地.
Usually you will have the name and id attributes with the same value for easy tracking (instead of referring to an input once by id and then by name); I used different values to reinforce the difference. And off course you will have the source in one form and the destination in another.
<form name="form1" ...>
...
<input name="source" id="source" type="text" value="" />
</form>
<form name="form2" ...>
...
<input name="destination" id="destination" type="hidden" value="" />
</form>
这篇关于当其中一种发生onchange时,以两种形式发布参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!