Visualforce-将Javascript用于必填字段

时间:2018-09-18 22:36:08

标签: salesforce

团队,

我正在使用Javascript验证Salesforce Visualforce页面上的必填字段-选择列表字段和文本字段。我尝试使用html required = true 但这不起作用,因为在我的visualforce页面上有两个功能-保存链接和提交按钮。如果用户尝试提交并且必填字段为空/空白,我希望用户收到错误消息。以下是我的代码,但无法正常工作。

<script>
	function submitOnClick(objSubmitBtn){
		var RevPL = document.getElementById('RevPL').value;
		if(RevPL == null || RevPL == ''){
			alert('Review needed.')
			return false;
		}

		var StatusPL = document.getElementById('StatusPL').value;
		if(StatusPL == null || AdhStatusPL == ''){
			alert('Status needed.')
		}
		
		var StatusPL = document.getElementById('Notes').value;
		if(StatusPL == null || AdhStatusPL == ''){
			alert('Please add notes.')
		}

		if(confirm('Are you sure you want to submit?')){
			objSubmitBtn.disabled = true;
			objSubmitBtn.value = 'Submitting... Please Wait';
			doSubmit();
		}
	}
</script>

<apex:form id="PRDetails"> 
	<table width="100%">
		<tr>
			<td width="50%">
				3. Review of documentation &nbsp;
					<b> <apex:outputText value="{!PR['Review__c']}" rendered="{!PR['Status__c']='Complete'}"/> </b>
						<apex:inputField value="{!PR['Review__c']}" rendered="{!PR['Status__c']!='Complete'}" id="RevPL" />
				
			</td>
		</tr> 
	</table>
	
	<br/>  
	
	<table width="100%">
		<tr>
			<td width="50%">4. Status &nbsp;
				<b> <apex:outputText value="{!PR['Status__c']}" rendered="{!PR['Status__c']='Complete'}"                                      /></b>
					<apex:inputField value="{!PR['Status__c']}" rendered="{!PR['Status__c']!='Complete'}" id="StatusPL" />
			</td>
		</tr>
		<tr>
			<td class="padding">Notes:</td> 
		</tr>
		<tr>
			<td class="padding">
				<b> <apex:outputText value="{!PR['Notes__c']}" rendered="{!PR['Status__c']='Complete'}"/> </b>
					<apex:inputField styleClass="inputLTextbox" value="{!PR['Notes__c']}" rendered="{!PR['Status__c']!='Complete'}" id="Notes"/>
			</td>
		</tr>
	</table>
	
	<table align="center">
        <tr>
            <td>
                <apex:repeat value="{!PR}" var="PR">
                    <apex:commandButton value="Submit" style="background: #1895c1; color: #FFFFFF; width: 150px;" onclick="submitOnClick(this);" rendered="{!PR['Status__c'] !='Complete'}"/> 
                </apex:repeat>
                <apex:actionFunction name="doSubmit" action="{!submitPR}" /> 
            </td>
        </tr>
    </table>
	
    <table width="100%">
        <tr>
            <td style="text-align:right;"><apex:commandLink action="{!savePR}" value="Save" id="SaveLink" style="text-decoration:none; color:blue;"/></td>
        </tr>
    </table>
</form> 

1 个答案:

答案 0 :(得分:0)

1)您已经

var StatusPL

两次定居。

2)Visualforce以自己的方式生成ID(向其添加层次结构)-您的“ getElementById”将不起作用,您需要做其他事情-在浏览器中的页面上进行检查。也许添加类并改用getElementByClass?