【问题标题】:How to erase p:output value by JS如何通过JS擦除p:输出值
【发布时间】:2017-06-27 15:30:28
【问题描述】:

我遇到了这样的问题 - 当用户通过 jsf 函数清除相关字段时,我试图在 jsf 页面上隐藏错误消息,但它不起作用

这是我调用 jsf 函数的 jsf 页面中的 sn-p

<div class="item">
<p:outputLabel for="phone" value="#{msgs['customerForm.phone']}"/>
<p:inputText id="phone" value="#{customerBean.customer.mobileNum}"
placeholder="380*********"required="true" requiredMessage="#{msgs['Error.phone.mandatory']}"
validatorMessage="#{msgs['Error.phone.wrongFormat']}">
<p:ajax event="keyup" oncomplete="hideCustomerErrMsg('phone')"/>
<f:validateRegex pattern="^380[\d]{9}$"/>
<f:ajax update="login"/>
<p:ajax event="change" update="login"/>
</p:inputText><p:message id="m_phone" for="phone" display="text"/>
</div>

这是我的 jsf 页面中的 sn-p - 当用户动态清除字段时我想隐藏的项目

<div class="errorMessages">
<h:outputText id="f_phone" value="#{msgs['Error.phone.duplicate']}" rendered="#{customerBean.mobileDuplicate}"
styleClass="ui-message-error"/>
 <p:spacer width="20"/>
 </div>

这是我的 js 函数

function hideCustomerErrMsg(variable) {
    var inputField, msg, span;
    inputField =document.getElementById("createEditCustomerForm:accordion:" + variable);
    msg = document.getElementById("createEditCustomerForm:accordion:m_" + variable);
    span = document.getElementById("createEditCustomerForm:f_" + variable).getElementsByTagName("span");
    if (inputField.value === "" && msg.hasChildNodes()) {
        msg.innerHTML = "";
        span.innerHTML ="";
    }}

【问题讨论】:

    标签: javascript java jsf


    【解决方案1】:

    您需要检查 - 1.打开F12开发者工具,调试你的函数在用户清除字段时是否正在调用 2. 只需仔细检查您提供的 id 是否存在于当前 dom 上。 3. getElementsByTagName 返回所有匹配元素的集合。您必须遍历它们并为每个元素执行 innerHtml='';

     for(var key in span)
        {
            if(span[key])
         span[key].innerHTML='';
        }
    

    【讨论】:

    • 如果我只使用 span = document.getElementById("createEditCustomerForm:f_" + variable) 会怎样
    • 有可能吗?因为 span.innerHTML ="";不工作(
    • 它应该可以正常工作。能否请您添加一些测试警报并检查,以便我们知道实际的 if 条件是否正在执行。
    • 函数 hideCustomerErrMsg(variable, label) { var inputField, msg, err, d; inputField = document.getElementById("createEditCustomerForm:accordion:" + 变量); msg = document.getElementById("createEditCustomerForm:accordion:m_" + 变量); err = document.getElementById("createEditCustomerForm:accordion:" + label); d = document.getElementById("createEditCustomerForm:f_" + variable) if (inputField.value === "" && msg.hasChildNodes()) { d.innerHTML= "" msg.innerHTML = ""; }}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 2021-01-12
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多