[英]How to find dropdown's selected option's index/value using javascript, before the selection change
I am not sure if the title of my question is meaningful enough, so here is my problem : I have a dropdown dynamically generated from server side as below: 我不确定我的问题的标题是否足够有意义,所以这是我的问题:我从服务器端动态生成了一个下拉列表,如下所示:
sb.append("<SELECT NAME='selectedQAStatuses' id='selectedReleaseQAStatuses' onchange='javascript:changeReleaseQAStatus(\""+releaseId+"\",\""+depEventSeq+"\",this);' >");
sb.append("<OPTION>Please select a QA status...</OPTION>");
for (String status : statusArray) {
if(status.equals(releaseStatus))
sb.append("<OPTION VALUE='"+status+"' selected>"+status+"</OPTION>");
else
sb.append("<OPTION VALUE='"+status+"'>"+status+"</OPTION>");
}
Now javascript : changeReleaseQAStatus() is getting called. 现在,javascript:changeReleaseQAStatus()被调用。 In this javascript method I am calling a confirm() to see if user is sure or not. 在此javascript方法中,我正在调用Confirm()以查看用户是否确定。 If user selects "OK" in the confirm popup then from the "IF" block i make a Ajax call etc., but if user selects "Cancel" in the confirm popup, then from the "ELSE" block I return from the JS method. 如果用户在确认弹出窗口中选择“确定”,则从“ IF”块中进行Ajax调用等操作,但是如果用户在确认弹出窗口中选择“取消”,则从“ ELSE”块中返回JS方法。
However, in "Cancel" scenario i see that although the user seems to have "Cancelled" his selection, still on the browser screen the dropdown seems to show the new option as "selected". 但是,在“取消”方案中,我看到尽管用户似乎已“取消”了他的选择,但仍在浏览器屏幕上,该下拉菜单似乎将新选项显示为“已选择”。 Therefore I think, in Javascript's "ELSE" block i should write something which should revert the selection to the original selection. 因此,我认为,在Javascript的“ ELSE”块中,我应该写一些东西,将选择内容还原为原始选择内容。 How am I supposed to 'remember' what was the original value of the dropdown when the page had rendered on the browser, so that I can user this initial value in my 'ELSE' block to restore my dropdown. 当页面在浏览器上呈现后,我应该如何“记住”下拉菜单的原始值,以便可以在“ ELSE”块中使用该初始值来恢复下拉菜单。
My Javascript to handle 'OK' and 'CANCEL': 我的Javascript处理“确定”和“取消”:
function changeReleaseQAStatus(releaseSeq, depEventSeq, element1){
var selectedIndex = element1.selectedIndex;
var selectedStatus = element1.options[selectedIndex].value;
if (confirm("Do you want to change Release QA status to \"" + selectedStatus + "\" ?")){
url = contextPath + "/changeQAStatusOfRelease.action?selectedStatus="+selectedStatus+"&releaseSeq="+releaseSeq+"&depEventSeq="+depEventSeq;
ajaxUpdate({url:url,
elementId:'compReleasesDtlsDiv',
});
}else{
/// No Code needed to execute here...
}
}
by using javascript you can use something like this 通过使用JavaScript,您可以使用类似这样的东西
var arr = document.getElementsById('select_id');
alert(arr[0].options[arr[0].selectedIndex].value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.