【问题标题】:Calling different action on same button using jquery使用 jquery 在同一个按钮上调用不同的操作
【发布时间】:2011-10-20 14:54:37
【问题描述】:

我在 certHolderAddressChange 屏幕上有一个保存按钮,如下所示:

<p:commandButton id="cmdSave" value="#{label.Save}" action="save" update="certHolderAddressChange" />

在此屏幕中有一个复选框。要求是:选中此复选框并单击“保存”按钮时,会打开一个弹出窗口。弹出窗口包含 2 个按钮:是和否。单击是按钮时,它会调用不同的操作。这是弹出代码:

<p:dialog header="#{label.primaryAdd}" widgetVar="dlg1" showEffect="bounce" hideEffect="explode" appendToBody="true">
    <h:panelGrid columns="2">
        <h:outputText value="#{label.msgPrimary}" />
        <p:spacer width="30" height="10" />
        <h:outputText />
        <p:spacer width="30" height="10" />
    </h:panelGrid>
    <div align="center">
        <p:commandButton immediate="true" value="#{label.yes}" action="makeAdd"/>
        <p:spacer width="10" height="5" />
        <p:commandButton value="#{label.no}" onclick="dlg1.hide()" type="button"/>
    </div>
</p:dialog>

如果未选中复选框并单击保存按钮,则会调用简单的"save" 操作(不打开弹出窗口)。为了实现这一点,我使用 jQuery 并根据复选框触发操作。这是jQuery代码:

<div>
    <h:inputHidden value="#{certHolderDetail.primaryInd}" id="primaryID" />
</div>
<script>
    jQuery("#cmdSave").click(function(e) {
        var textvalue = jQuery('#primaryID').val();
        if (textvalue == 'true') {
            dlg1.show();
        } else {
            jQuery("#save").click();
        }
    });                 
</script> 

它正在工作,但问题是在每种情况下都会调用 "save" 操作。但是我是否从按钮中删除了action="save",然后其他部分不起作用。只有 if 条件在 jQuery 代码中有效。

我想根据条件调用"save""makeAdd" 操作。我怎样才能做到这一点?

同样在每个条件下(复选框选中或未选中)每次都会触发“保存”操作。我想在选中复选框时停止保存操作.....

【问题讨论】:

    标签: jquery jsf primefaces


    【解决方案1】:

    不确定您使用的是哪个版本的 jQuery

    1.6+ 这就是你想要的

    jQuery("#cmdSave").click(function(e) {
            var textvalue = jQuery('#primaryID').prop('checked');
            if (textvalue == true) {
                dlg1.show();
            } else {
                jQuery("#save").click();
            }
        }); 
    

    jQuery("#cmdSave").click(function(e) {
            var textvalue = jQuery('#primaryID').attr('checked');
            if (textvalue == 'checked') {
                dlg1.show();
            } else {
                jQuery("#save").click();
            }
        }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多