将 SF 变量值传递给 jQuery 值以在 link 中使用

Passing SF variable value to jQuery value to use in link

我在这个问题上需要一些帮助,因为我已经在线搜索并尝试了将近三个小时的不同方法,但到目前为止没有任何效果。

这是我想要做的: 我构建了一个使用自定义 css 的自定义 VF 页面。页面部分包含在组件中。在我的 header 组件中,我有一个 HTML link 格式看起来像一个按钮。当用户单击该按钮时,他们应该被带回他们来自的机会记录。我无法使用 jQuery 检索机会 ID,无论我尝试了多少 times/things,这就是 link 的样子 (/%7B! oppId%7D)

对于此事,我将不胜感激,因为我似乎被卡住了!

谢谢:)

页面代码:

<apex:page standardController="Opportunity" extensions="DEMO_ScheduleOpportunityController" showHeader="false" sidebar="false" standardStylesheets="false">
<head>
</head>
<body>
    <header id="header-section">
        <c:DEMOscheduleHeader opportunity="{!opportunity}" />
    </header>
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js" />
    <apex:includeScript value="{!URLFOR($Resource.DEMO_Schedule, 'js/page.js')}" />
</body>
</apex:page>

组件代码:

<apex:component>
<apex:attribute name="opportunity" type="Opportunity" required="true" description="This is Opportuinity used by the Schedule" />
    <a href="#" id="exit" class="btn">Exit</a>
</apex:component>

控制器代码:

public with sharing class DEMO_ScheduleOpportunityController {
    public Opportunity opportunity {get; set;}

    public DEMO_ScheduleOpportunityController(ApexPages.StandardController controller) {
        Initialize();
    }
    public void Initialize() {
        opportunity = [SELECT Id FROM Opportunity WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
}

JS 代码:

$(document).ready(function() {
    var oppId = '{!oppId}'; // the Opportunity Id
    var exitURL = "/" + oppId; // creating and forming the exitURL
    document.getElementById('exit').href = exitURL; // assigning the exitURL to the Exit Button
});

apex:includeScript组件用于引入静态资源。也就是说,它不会合并来自顶点控制器的变量。

您可以直接在 Visualforce 页面代码中设置 Javascript 中的变量。

例如

<script type="text/javascript">
    var orgId = '{!opportunity.Id}';
</script>

如果您将 loadOnReady="true" 属性添加到 includeScript,您将能够在静态资源中使用 orgId