The problem is that the JSP page automatically submits and transfers me to the action servlet. How do I make it wait until the user makes a selection in the dropdown option menu?
<form name="mainForm" action="psm.srv" method="POST" onSubmit="return setParam(event)">
<select id="main" onchange="document.mainForm.submit()">
<option value="null">Choose chart</option>
<option value="pays">PAYS</option>
<option value="expenses">EXPENSES</option>
</select></form>
<script type="text/javascript">
function setParam(event){
event.preventDefault();
var select = document.getElementById("main");
var value = select.options[select.selectedIndex].value;
document.mainForm.action = "psm.srv?optionSelected=" + value ;
document.mainForm.submit();
}
</script>
The problem is this line:
onchange="document.mainForm.submit()"
What that means is that whenever the select input changes, the form will submit. You don't have to manually set this value. Add a 'name' attribute to the select field and just get the parameter in your servlet like you normally do. The value gets set automatically. You need a submit button though.
<form name="mainForm" action="psm.srv" method="POST">
<select name="selectField">
<option value="null">Choose chart</option>
<option value="pays">PAYS</option>
<option value="expenses">EXPENSES</option>
</select>
<input type="submit" value="Submit"/>
</form>
In your servlet you simply do:
String fieldValue = request.getParameter("selectField");
Remove on submit and onchange method and You can do simply by Jquery :
$(document).on('change', '#main', function () {
var selectedValue=$(this).val();
var url = "psm.srv?optionSelected=" + selectedValue;
window.location = url;
});
This will get your current value and take to the action url.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.