简体   繁体   English

在更改选择之前,如何使用javascript查找下拉列表的所选选项的索引/值

[英]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.

相关问题 如何根据所选选项的值更改选择的选项? - How to change select's option based on a selected option's value? 如何使用JQuery通过选项列表索引号更改JQuery-select2下拉列表的选定值 - How to change selected value of JQuery-select2 dropdown by option list Index number using JQuery 检查下拉列表的选定选项是不是第一个使用JavaScript的选项 - Check if dropdown's selected option is not the first with JavaScript 如何使用JavaScript将所选选项的值与选项文本相同 - How to manipulate selected option's value to be the same as option text with JavaScript 在使用下划线和主干选择下拉列表值时发生更改事件 - On change event on the selection of dropdown's value using underscore and backbone 在下拉菜单中更改所选的选项值 - Change selected option value in dropdown 使用jQuery或Javascript查找下拉选项值 - Using jQuery or Javascript to find dropdown option value React-Select:如何在选定值 onChange 上更新选定选项下拉列表的默认值? - React-Select: How do I update the selected option dropdown's defaultValue on selected value onChange? 使用JavaScript更改DDSlick下拉列表的选定值 - Change selected value of DDSlick dropdown using JavaScript 如何使用javascript设置下拉列表的选定索引 - How to set the selected index of a dropdown using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM