0

我想在 jQuery 中写一个 if else 语句。如果单击按钮,请禁用按钮,直到操作完成,然后才重新启用。这是我目前拥有的代码。当我导航到页面时,动作不停​​地循环。请多多包涵,因为我还是 jQuery 的新手。

仅供参考,我正在使用 primefaces 命令按钮

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:hx="http://www.ibm.com/jsf/html_extended">
<h:head>
    <title>ma1009.xhtml</title>
    <meta http-equiv="keywords" content="enter,your,keywords,here" />
    <meta http-equiv="description"
    content="A short description of this page." />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

    <script src="../../jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {

            if ($("#button").click()) {
                $("#button", this).prop('disabled', true);
            } else {
                $("#button", this).prop('disabled', false);
            }

        });
    </script>
</h:head>

<h:body>
    <h:form id="form1" enctype="multipart/form-data" prependId="false">

        <p:commandButton id="button" type="submit" value="Submit"
            action="#{pc_Ma1009.doSubmitAction}" ajax="false"></p:commandButton>

    </h:form>
</h:body>
</html>
4

2 回答 2

2

它应该是这样的:

 $(document).ready(function(e) {
    e.preventDefault();
    $("#button").click(function(){
        $(this).prop("disabled", true);
        //your action (maybe function pc_Ma1009.doSubmitAction()), look at your html generated by <p:commandButton id="button"
        $(this).prop("disabled", false);
    });

});
于 2013-07-18T06:23:23.497 回答
0

我设法用以下代码得到它。@ABFORCE 感谢您的帮助!^^ @Alexey Aza 感谢您的帮助!^^

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:hx="http://www.ibm.com/jsf/html_extended">
<h:head>
    <title>ma1009.xhtml</title>
    <meta http-equiv="keywords" content="enter,your,keywords,here" />
    <meta http-equiv="description"
        content="A short description of this page." />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

    <link type="text/css" rel="stylesheet"
        href="#{request.contextPath}/theme/primefaces-aristo/theme.css" />

    <script src="../../jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("button").click(function() {
                $(this).css('background', 'red');
                $(this).addClass('ui-state-disabled');  
            });
        });
    </script>
</h:head>

<h:body>
    <h:form id="form1" enctype="multipart/form-data" prependId="false">

        <p:commandButton id="button" type="submit" value="ma1009" styleClass="commandButton"
            action="#{pc_Ma1009.doSubmitAction}" widgetVar="ajaxStatusDialog">
            <p:ajaxStatus onstart="ajaxStatusDialog.show();" onsuccess="ajaxStatusDialog.hide();"></p:ajaxStatus>

        </p:commandButton>

    </h:form>
</h:body>
</html>
于 2013-07-19T04:04:49.613 回答