简体   繁体   English

如何从中获取所选项目 <h:selectOneMenu> 在JavaScript中

[英]How to get selected item from <h:selectOneMenu> in javascript

I'm trying to get the selected item from my selectOneMenu onchange but it's not working. 我正在尝试从selectOneMenu onchange获取选定的项目,但是它不起作用。 Every time I change the drop down and select another option, the console on my web browser says 每次更改下拉菜单并选择另一个选项时,Web浏览器上的控制台都会显示

The value of the property 'getOffset' is null or undefined, not a Function object 属性“ getOffset”的值为null或未定义,不是Function对象

This is my .xhtml page named ProgramDetails.xhtml 这是我的.xhtml页面,名为ProgramDetails.xhtml

<h:dataTable id="DispatchConfigurationCustom" columnClasses="portlet-table-same portlet-table-cell" headerClass="portlet-table-same portlet-table-cell" value="#{CRUDOperatorProgram.workflowStepList}" var="workflowConfig" width="100%">
<h:column>
    <f:facet name="header">
        <h:outputText value="Include" />
    </f:facet>
    <h:selectBooleanCheckbox id="includeInd" value="#{workflowConfig.isIncludedInd}"/>
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Automate" />
    </f:facet>
    <h:selectOneRadio id="onOff" value="#{workflowConfig.isAutomatedInd}">
        <f:selectItem id="onButton" itemLabel="On" itemValue="1" />
        <f:selectItem id="offButton" itemLabel="Off" itemValue="0" />
    </h:selectOneRadio>
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Action Step" />
    </f:facet>
    <h:outputText value="#{workflowConfig.workflowStep.displayLabel}" />
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Offset Prior" />
    </f:facet>
    <label for="offsetPriorBoxHour"><h:outputText value="hrs:" rendered="#{workflowConfig.workflowStep.displayLabel == 'Curtail Notification'}" /></label>
    <h:selectOneMenu styleClass="portlet-dropdown"  rendered="#{workflowConfig.workflowStep.displayLabel == 'Curtail Notification'}"  id="offsetPriorBoxHour" value="#{workflowConfig.offsetMinutes}" onchange="getOffset(this);">
        <f:selectItems value="${CRUDOperatorProgram.hourList}" />
    </h:selectOneMenu>
    <label for="offsetPriorBoxMin"><h:outputText value="min:" rendered="#{workflowConfig.workflowStep.displayLabel == 'Curtail Notification' or workflowConfig.workflowStep.displayLabel == 'Multisite Follow Up' or workflowConfig.workflowStep.displayLabel == 'Curtail Toggle'}" /></label>
    <h:selectOneMenu styleClass="portlet-dropdown"  rendered="#{workflowConfig.workflowStep.displayLabel == 'Curtail Notification' or workflowConfig.workflowStep.displayLabel == 'Multisite Follow Up' or workflowConfig.workflowStep.displayLabel == 'Curtail Toggle'}"  id="offsetPriorBoxMin" value="#{workflowConfig.offsetMinutes}" onchange="getOffset(this);">
        <f:selectItems value="${CRUDOperatorProgram.minuteList}" />
    </h:selectOneMenu>
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Offset After" />
    </f:facet>
    <label for="offsetAfterBoxMin"><h:outputText value="min:" rendered="#{workflowConfig.workflowStep.displayLabel == 'Restore Notification' or workflowConfig.workflowStep.displayLabel == 'Overlapping Follow Up' or workflowConfig.workflowStep.displayLabel == 'Restore'}" /></label>
    <h:selectOneMenu styleClass="portlet-dropdown"  rendered="#{workflowConfig.workflowStep.displayLabel == 'Restore Notification' or workflowConfig.workflowStep.displayLabel == 'Overlapping Follow Up' or workflowConfig.workflowStep.displayLabel == 'Restore'}"  id="offsetAfterBoxMin" value="#{workflowConfig.offsetMinutes}" onchange="getOffset(this);">
        <f:selectItems value="${CRUDOperatorProgram.minuteList}" />
    </h:selectOneMenu>
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Offset Target" />
    </f:facet>
    <h:outputText rendered="#{workflowConfig.workflowStep.displayLabel == 'Curtail Notification' or workflowConfig.workflowStep.displayLabel == 'Multisite Follow Up' or workflowConfig.workflowStep.displayLabel == 'Curtail Toggle'}"  id="offsetTargetStartBox" value="Start" />
    <h:outputText rendered="#{workflowConfig.workflowStep.displayLabel == 'Restore Notification' or workflowConfig.workflowStep.displayLabel == 'Overlapping Follow Up' or workflowConfig.workflowStep.displayLabel == 'Restore'}"  id="offsetTargetEndBox" value="End" />
</h:column>
<h:column>
    <f:facet name="header">
        <h:outputText value="Offset Summary" />
    </f:facet>
    <h:outputText rendered="#{workflowConfig.workflowStep.displayLabel == 'Curtail Notification' or workflowConfig.workflowStep.displayLabel == 'Multisite Follow Up' or workflowConfig.workflowStep.displayLabel == 'Curtail Toggle'}"  id="offsetSummaryBeforeBox" value="before Start time" />
    <h:outputText rendered="#{workflowConfig.workflowStep.displayLabel == 'Restore Notification' or workflowConfig.workflowStep.displayLabel == 'Overlapping Follow Up' or workflowConfig.workflowStep.displayLabel == 'Restore'}"  id="offsetSummaryAfterBox" value="after End time" />
</h:column>
<script>
    function getOffset( dropdown ) {
        var minutesTemp = dropdown.options[dropdown.selectedIndex].value;
        var minutes = minutesTemp.toString();
        alert("Minutes: " + minutes);
    // <![CDATA[
        if(document.getElementById('ProgramDetails:DispatchConfigurationCustom:0:offsetPriorBoxHour') != null) {
            var hourBox = document.getElementById('ProgramDetails:DispatchConfigurationCustom:0:offsetPriorBoxHour');
            var hourTemp = hourBox.options[hourBox.selectedIndex].value;
            var hour = hourTemp.toString();
            document.getElementById('ProgramDetails:DispatchConfigurationCustom:0:offsetSummaryBeforeBox').value = hours + " hours and " + minutes + " minutes before Start time";
        } else if(document.getElementById('ProgramDetails:DispatchConfigurationCustom:0:offsetSummaryBeforeBox') != null) {
            document.getElementById('ProgramDetails:DispatchConfigurationCustom:0:offsetSummaryBeforeBox').value = minutes + " minutes before Start time";
        } else { 
            document.getElementById('ProgramDetails:DispatchConfigurationCustom:0:offsetSummaryAfterBox').value = minutes + " minutes before Start time";
        }
        //]]>
    }

</script>
</h:dataTable>  

These are my hourList and minuteList methods that are populating the selectOneMenu... 这些是我的hourList和minutesList方法,它们正在填充selectOneMenu ...

private void loadMinuteList() {
    minuteList = new ArrayList<SelectItem>();
    for(Integer i=0; i<=60; i++){
        minuteList.add(new SelectItem(i, i.toString()));
    }
}

private void loadHourList() {
    hourList = new ArrayList<SelectItem>();
    for(Integer i=0; i<=30; i++){
        hourList.add(new SelectItem(i, i.toString()));
    }
}

Sorry for all the long code blocks. 对不起,所有长代码块。 I'm just trying to give you the most information I can. 我只是想为您提供尽可能多的信息。 Is there something wrong with the way I am using javascript? 我使用JavaScript的方式有问题吗? Or the onchange=""? 还是onchange =“”?

You have 2 issues in your script : 您的脚本中有2个问题:

var minutesTemp = dropdown.options[dropdown.selectedIndex].value.;

Remove the additional . 删除其他. at the end 在末尾

and

else (document.getElementById('ProgramDetails:DispatchConfigurationCustom:0:offsetSummaryBeforeBox') != null) {
   document.getElementById('ProgramDetails:DispatchConfigurationCustom:0:offsetSummaryBeforeBox').value = minutes + " minutes before Start time";
}

should be 应该

else if (document.getElementById('ProgramDetails:DispatchConfigurationCustom:0:offsetSummaryBeforeBox') != null) {
   document.getElementById('ProgramDetails:DispatchConfigurationCustom:0:offsetSummaryBeforeBox').value = minutes + " minutes before Start time";
}

ie add an if after the else - you cannot check a condition when using else unless you include if too 即添加ifelse使用的时候可以不检查一个条件- else ,除非你有if

Please try this: 请尝试以下方法:

<h:selectOneMenu styleClass="portlet-dropdown"  rendered="#{workflowConfig.workflowStep.displayLabel == 'Restore Notification' or workflowConfig.workflowStep.displayLabel == 'Overlapping Follow Up' or workflowConfig.workflowStep.displayLabel == 'Restore'}"  id="offsetAfterBoxMin" value="#{workflowConfig.offsetMinutes}" onchange="getOffset(offsetAfterBoxMin[index].value);">
    <f:selectItems value="${CRUDOperatorProgram.minuteList}" />
</h:selectOneMenu>

and remove the : var minutesTemp = dropdown.options[dropdown.selectedIndex].value; 并删除: var minutesTemp = dropdown.options[dropdown.selectedIndex].value; OR 要么

<h:selectOneMenu styleClass="portlet-dropdown"  rendered="#{workflowConfig.workflowStep.displayLabel == 'Restore Notification' or workflowConfig.workflowStep.displayLabel == 'Overlapping Follow Up' or workflowConfig.workflowStep.displayLabel == 'Restore'}"  id="offsetAfterBoxMin" value="#{workflowConfig.offsetMinutes}">
    <f:selectItems value="${CRUDOperatorProgram.minuteList}" />
    <f:ajax event="valueChange" execute="getOgetOffset(offsetAfterBoxMin[index].value);"/>
</h:selectOneMenu>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM