簡體   English   中英

使用Java啟用/禁用按鈕

[英]Enabling/Disabling button with Javascript

問題:我在VisualForce頁面上有一個apex:commandButton,並且其中包含一些邏輯的js文件。 當前,此邏輯使用CSS影響按鈕在頁面上是可見還是不可見。 我需要更改此設置,以使其啟用或禁用按鈕。 但是,每當我嘗試禁用js文件中的按鈕時,屏幕上的按鈕都不會受到影響。 我對前端開發人員沒有經驗,因此可能會丟失或誤解一些明顯的東西。

.page文件上的按鈕:

    <apex:commandButton id="commitButton"
        action="{!commitSelectedLines}"
        value="{!$Label.commitSelectedLines}"/>

.js文件(已大幅度地修改為看似相關的位):

FFDCBalancer = (function(){

    /**
     * Singleton Balancer object to maintain balances on the page.
     */
    var balancer = {

        /** JQuery button component for the commit button. */
        cmdCommit: undefined,

        /** Set the enabled-ness of the commit button. */
        setCommitEnabled : function(value) {
            //I PRESUMABLY NEED TO CHANGE THIS BIT TO USE 'DISABLED'.
            this.cmdCommit.css({
               'display': value ? 'block' : 'none'
            });

            //I HAVE TRIED VARIOUS BITS OF CODE, SUCH AS THIS
            //this.cmdCommit.disabled = value;

        },

        /**
         * Respond to refresh by connecting event handlers and calculating the balances.
         */
        onRefresh : function () {
            var me = this;
            me.cmdCommit = $FFDC('#commitButton');
        }
    };

    $FFDC(document).on('FFDCRefresh', function(){ balancer.onRefresh(); });
    return balancer;
}());

為此,您可以執行以下兩項操作之一:

  1. 使用{!$ Component ... commitButton}來獲取客戶端ID,盡管它確實會使JQuery混亂,因為它在ID之間添加了一個冒號,因此您必須將其與document.getElementById結合使用(...) - 例如:

    jQuery(document.getElementById('{!$Component.Form.commitButton}')).attr('disabled','disabled');

  2. 在按鈕上放置一個“ styleClass”,並使用該樣式進行更改:

    <apex:commandButton id="commitButton" action="{!commitSelectedLines}" value="{!$Label.commitSelectedLines}" styleClass="commitButtonClass" />

然后使用jQuery設置屬性:

jQuery(".commitButtonClass").attr('disabled','disabled');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM