简体   繁体   English

在PrimeFaces p:datatable中禁用图像提交按钮

[英]Disable image submit button in PrimeFaces p:datatable

I have a DataTable with a CommandButton which acts like this: 我有一个带有CommandButton的DataTable,其行为如下:

<h:commandButton action="#{communicator.requestFavoriteDetails}"
onclick="disableSubmit(this.id);"
update="@navi:tableFavorites">
<f:setPropertyActionListener value="#{book}"
target="#{favorites.selectedFavorite}" />
<f:ajax />  
</h:commandButton>

(I just removed some tags...) (我刚刚删除了一些标签...)

This button works fine. 此按钮工作正常。 But on some (mobile) devices, a request is sent twice on click. 但是在某些(移动)设备上,每次点击都会发送两次请求。 I don`t know why.. I tried everything but nothing worked to prevent this. 我不知道为什么。我尝试了一切,但没有任何方法可以防止这种情况。 (eg ) (例如)

This code is rendered to an: tag and I know this can not be disabled. 这段代码呈现为一个:标记,我知道这不能被禁用。

I want to add a timer. 我想添加一个计时器。 When a user clicks on this button the "onclick" event should be replaced with an empty function and after 2.. seconds it should be restored. 当用户单击此按钮时,应将“ onclick”事件替换为空函数,并且应在2.秒后恢复该事件。 This way I can prevent a request to be sent twice. 这样,我可以防止将请求发送两次。

The function disableSubmit(id) acts like this: 函数disableSubmit(id)的行为如下:

function disableSubmit(id) {
    var new_func = "function() { return false }";
    if ( typeof(document.getElementById(id)) == "object" && typeof(document.getElementById(id).onclick) == "function") {
        if ($("#" + escapeJSFid(id)).attr('onclick') != new_func) {
        var old_func = $("#" + escapeJSFid(id)).attr("onclick");
        $("#" + escapeJSFid(id)).attr('onclick','"+new_func+"').delay(2000).attr('onclick','"+old_func+"');
        }
    }
}

But this code does not work. 但是此代码不起作用。 FF does not return any errors.. Oh.. and escapeJSFid is just a simple function: FF不返回任何错误..哦.. andscapeJSFid只是一个简单的函数:

function(id){ id.replace(/:/g,"\\:"); }

Does anybody know how I can prevent this request to be sent twice? 有谁知道我可以防止两次发送此请求? I know there are many other solutions and my code is poorely written.. but I just want it to work. 我知道还有许多其他解决方案,并且我的代码编写得很差..但是我只是希望它能工作。

Best regards 最好的祝福

Seems like I solved my problem: 好像我解决了我的问题:

function disableSubmit(id) {
    $("#" + escapeJSFid(id)).click( function() {
            // alert ("DISABLED");
        return false;
    });
}

Onclick: Function disableSubmit(id) will be executed. Onclick:将执行功能disableSubmit(id)。 The onClick function will be removed. onClick功能将被删除。 But, I guess PrimeFaces datatable stores its inforamtions about buttons etc. in a bean (somewhere). 但是,我猜PrimeFaces数据表将其关于按钮等的信息存储在Bean中(某处)。 The buttons are getting restored automatically with their old values and events (onclick). 这些按钮会自动恢复其旧值和事件(onclick)。 When I doubleclick my button I see the DISABLED alert message. 当我双击我的按钮时,我看到DISABLED警报消息。 But when I click on the button, wait 2-3 seconds and click then again on the button no message appears. 但是,当我单击该按钮时,请等待2-3秒,然后再次单击该按钮,则不会出现任何消​​息。 The normal function is being executed. 正常功能正在执行。 So I guess my button gets restored automatically after the "action=''" event is completed. 因此,我猜我的按钮在“ action =”事件完成后会自动恢复。

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

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