简体   繁体   English

改变<p:commandButton> JavaScript 的价值

[英]Change <p:commandButton> value with JavaScript

Is there any way to change a <p:commandButton> value with Javascript?有没有办法用 Javascript 更改<p:commandButton>值?

I tried to set a widgetVar in the <p:commandButton> but it doesn't have any setValue() function or something like this.我试图在<p:commandButton>设置一个widgetVar ,但它没有任何setValue()函数或类似的东西。 I can't bind it to a managed bean due to other specific issues with my XHTML.由于 XHTML 的其他特定问题,我无法将其绑定到托管 bean。

JSF generates HTML. JSF 生成 HTML。 JavaScript works with HTML. JavaScript 与 HTML 一起使用。 When writing JavaScript code, don't look too much at the JSF source code if you already can't tell form top of head what HTML code exactly it would generate.在编写 JavaScript 代码时,如果您已经无法告诉 form top 它将生成什么 HTML 代码,请不要过多地查看 JSF 源代码。

Open the JSF page in your favourite browser, rightclick and View Source .在您喜欢的浏览器中打开 JSF 页面,右键单击并查看源代码 The generated HTML output of a <p:commandButton value="foo" /> look like this: <p:commandButton value="foo" />生成的 HTML 输出如下所示:

<button id="formId:buttonId" name="formId:buttonId" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick="PrimeFaces.ab({formId:'formId',source:'formId:buttonId',process:'@all'});return false;" type="submit">
    <span class="ui-button-text">foo</span>
</button>

Look, the button has a single <span> child with therein the value.看,按钮有一个<span>子元素,其中包含值。

So, this piece of JS to change the button's value to bar should do:因此,这段将按钮的值更改为bar的 JS 应该执行以下操作:

document.getElementById("formId:buttonId").firstChild.innerHTML = "bar";

This is a better way using the widgetVar :这是使用 widgetVar 的更好方法:

lets say we set confirmWgt as the <p:commandButton> widgetVar, The javascript code to set its value will be :假设我们将confirmWgt设置为<p:commandButton> widgetVar,设置其值的javascript代码将是:

PF('confirmWgt').jq.text("New Value");

Using this way, you are sure if any changes in the html code applied by primefaces in the future will not cause any problem in updating its version.使用这种方式,您可以确定将来primefaces 应用的html 代码中的任何更改不会导致更新其版本的任何问题。

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

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