gpt4 book ai didi

javascript - 使用jQuery(或JSF方式)来防止h :commandButton submit

转载 作者:太空宇宙 更新时间:2023-11-04 16:13:49 24 4
gpt4 key购买 nike

我有一个 JSF 页面(已在 header 中注册 jQuery)包含 indicator , reason和一个 h:commandButton用于提交h:formindicator有两个值 YN

我尝试构建的逻辑是在页面加载时和 indicator = Y (在下面的 jQuery 中是 indicator.data("prev") == 'Y' ),如果用户从 Y 切换至N但不要在 reason 中输入任何单词,甚至用户单击 h:commandButton ,不应提交表单。

简而言之,在提交之前,应该检查指标当前值与前值关系的状态是否从 Y 转变为至N (我在jQuery脚本部分设置了这个逻辑部分),还需要检查原因不为空。 我猜要实现这样的检查操作可能需要重新组织代码结构。

下面是我当前的代码,它缺少这个逻辑,永远不会禁止提交并导致支持 bean 冲突。由于我是 jQuery 和 JSF 的新手,有人可以告诉我如何防止此提交吗?谢谢。

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.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:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">

<h:head>
<title>myPage</title>
<h:outputScript name="jquery.js" target="head"/>
</h:head>

<f:event type="preRenderView" listener="#{myPage.getmyPage}" />

<h:body>
<f:view>
<h:form name ="myPage" id ="myPage">
<table>
<tr>
<td align="left">
<h:selectOneMenu id="indicator" value="#{myPage.indicator}">
<f:selectItem itemValue="Y" itemLabel="YES" />
<f:selectItem itemValue="N" itemLabel="NO" />
</h:selectOneMenu>
</td>
<td>
<h:inputTextarea id="reason" value="#{myPage.reason}">
</h:inputTextarea>
</td>
<td>
<h:commandButton id="submit" value="Submit"
onclick="if (! confirm('Do you want to update?')) return false" action="#{myPage.update}">
</h:commandButton>
</td>
</tr>
</table>
</h:form>
</f:view>


// TODO: I want to add the logic to prevent h:commandButton submit(e.g disable the button) when reason is empty
// and indicator value turn from 'Y' to 'N', the jQuery section reflect the case when toggle from 'Y' to 'N',
// reason editable, as it need to consider two elements(indicator value/ reason value) at the same time,
// how should the code look like ?

<script type="text/javascript">
$(window).load(function() {
var indicator = $(document.getElementById("myPage:indicator"));
// set the indicator pre data
indicator.data("prev", indicator.val());

// Handle initial status of indicator, when page load, if it is 'Y' open reason field
// if it is 'N' disable reason field,
if(indicator.data("prev") == 'Y') {
$(document.getElementById("myPage:reason")).prop('disabled', false);
} else {
$(document.getElementById("myPage:reason")).prop('disabled', true);
}

// Handle change status of indicator,
indicator.change(function(data){
var jqThis = $(this);
var after_change = jqThis.val();
var before_change = jqThis.data("prev");
if(before_change == 'Y' &amp;&amp; after_change == 'N'){
$(document.getElementById("myPage:reason")).prop('disabled', false);
}
});
});
</script>});
</h:body>

最佳答案

基本上,您可以根据某些条件实现一些逻辑来禁用命令按钮。例如:

<h:commandButton id="submit" value="Submit" disabled=#{myPage.isSubmitButtonDisabled()}
onclick="if (! confirm('Do you want to update?')) return false" />

然后在支持 bean 中,您应该实现您想要实现的逻辑,例如:

public boolean isSubmitButtonDisabled() {
return indicator.equals("N") && reason.isEmpty();
}

最后,要处理指标选择的更改,您应该添加 onchange="submit()"给您<h:selectOneMenu>成分。按钮启用/禁用的逻辑可以通过以类似的方式对原因文本区域中键入的文本使用react来进一步扩展,例如通过 addig onkeyup="submit()" 。当然,这需要使用 render属性以防止 pafe 刷新。这当然是一种方法,但它可以为您指出令您满意的解决方案

关于javascript - 使用jQuery(或JSF方式)来防止h :commandButton submit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41232933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com