简体   繁体   English

禁用按钮的JavaScript设置属性

[英]JavaScript Setting property of a disabled button

PRE EDIT: It turned out that it was not about the disability of the button, but making some other actions after save. 预编辑:事实证明,这与按钮的禁用无关,而是在保存后进行了一些其他操作。 I debugged the page and found out that after making changes on a saved form, then page loses the javascript functionality in the (document).ready part. 我调试了页面,发现在保存的表单上进行更改后,页面失去了(document).ready部分中的javascript功能。 I've added the solution as an answer. 我已将解决方案添加为答案。


I have an entry page which has two buttons save and approve. 我有一个进入页面,其中有两个按钮保存和批准。 The mechanism is something like, you can fill the form and save, then approve. 该机制类似于,您可以填写表格并保存,然后批准。 You can also reach a saved page by refreshing the page or from the list of your saved pages. 您也可以通过刷新页面或从已保存页面列表中访问已保存页面。

The approve button is disabled if the form is not saved. 如果未保存表单,则批准按钮将被禁用。 I enable it from code behind after saving. 保存后,我会从代码后面启用它。 Approve button also has a confirm button extender which takes its confirm text from javascript. 批准按钮还具有一个确认按钮扩展器,该扩展器从javascript获取其确认文本。 I load it in (document).ready and its code is: 我将其加载到(document).ready ,其代码为:

$(document).ready(function () {
        $("#ASPxSplitter1_ContentPlaceHolder1_uctActivityEntry1_tbActivity_tbHistory_btnApproveActivity_btnApprove").click(function () {

            $("#ASPxSplitter1_ContentPlaceHolder1_uctActivityEntry1_tbActivity_tbHistory_btnApproveActivity_lblMessage").text(GetConfirmTextForApprove());
        });
    });

,where GetConfirmTextForApproval() makes some calculations and returns the confirm text. ,其中GetConfirmTextForApproval()进行一些计算并返回确认文本。

Now, my problem is, as the button is disabled when you open the form, the code above is not rendered at the first page load. 现在,我的问题是,因为当您打开表单时按钮被禁用,所以上面的代码不会在第一页加载时呈现。 This leads to the problem that, when I start to fill a form and save it, then approve it, I don't get any confirm text, because it does not run the function. 这就导致了一个问题,当我开始填写表格并保存然后批准时,我没有得到任何确认文本,因为它没有运行该功能。 But after refreshing the page or after I go to a saved form's page from another page, I get the proper confirm text. 但是刷新页面或从另一个页面转到保存的表单页面后,我会得到正确的确认文本。

So, how can I solve this problem? 那么,我该如何解决这个问题呢? How can I get the proper confirm text even though the button is disabled at the first page load? 即使在第一页加载时禁用了按钮,如何获得正确的确认文本?

Note: I have to add that after saving, the url of the page is changed. 注意:我必须补充一点,保存后页面的URL会更改。 The query string is added. 查询字符串已添加。 That might also cause the problem. 那也可能导致问题。

You can use 您可以使用

// Disable #x
$( "#x" ).prop( "disabled", true );

// Enable #x
$( "#x" ).prop( "disabled", false );

But not when document ready, you need enable button when you want. 但是在准备好文档时还没有,需要时需要启用按钮。 Then you need create a event listener 然后,您需要创建一个事件监听器

$("#button").click(function(){
  //Your code

  if(GetConfirmTextForApproval()){
  //You active the button and the text that you want show.
   }
});

Solved my own problem: 解决了我自己的问题:

As it was said in the pre edit of the question, the problem was caused because of making changes after the save. 正如在问题的预编辑中所说的那样,问题是由于保存后进行更改而引起的。 I've changed my function as: 我将功能更改为:

$(document).ready(function () {
    SetConfirmMessageForApproval();
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
});

function EndRequestHandler(sender, args) {
    SetConfirmMessageForApproval();
    }

function SetConfirmMessageForApproval() {
        $("#ASPxSplitter1_ContentPlaceHolder1_uctActivityEntry1_tbActivity_tbHistory_btnApproveActivity_btnApprove").click(function () {
            $("#ASPxSplitter1_ContentPlaceHolder1_uctActivityEntry1_tbActivity_tbHistory_btnApproveActivity_lblMessage").text(GetConfirmTextForApprove());
        });
    }

This helps, if anyone else needs it. 如果其他人需要它,这会有所帮助。

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

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