从 p:inputText javascript 获取值文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11558654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value text from p:inputText javascript
提问by cri_sys
I'm trying something simple, just to get the text value inside a p:inputText
thought Javascript, but I`m not getting it.
Perhaps it's a different procedure with Primefaces, since if I don't use that, I don't encounter the problem.
我正在尝试一些简单的方法,只是为了获取p:inputText
思想 Javascript 中的文本值,但我没有得到它。也许它是 Primefaces 的不同程序,因为如果我不使用它,我就不会遇到问题。
My code:
我的代码:
<p:inputText value="any text" widgetVar="youtlink" ></p:inputText>
<p:commandButton value="Search" onclick="loadPlayer();" icon="ui-icon-search" />
<script type="text/javascript">
function loadPlayer() {
alert(youtlink.text);
}
</script>
I have tried also with JQuery but also no success.
我也尝试过使用 JQuery 但也没有成功。
Rendered view:
渲染视图:
<form id="editlFrm" enctype="application/x-www-form-urlencoded" method="post"
name="editlFrm">
<input id="editlFrm:j_id_7" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all"
type="text" value="assd" name="editlFrm:j_id_7" role="textbox" aria-disabled="false"
aria-readonly="false" aria-multiline="false">
<button id="editlFrm:j_id_8" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left"
type="submit" onclick="loadPlayer();;PrimeFaces.ab({source:'editlFrm:j_id_8'});return false;"
name="editlFrm:j_id_8" role="button" aria-disabled="false">
采纳答案by Daniel
Add id
to your <p:inputText
添加id
到您的<p:inputText
like this
像这样
<p:inputText id="someID" value="any text" widgetVar="youtlink" ></p:inputText>
make sure your form got prependId="false"
确保你的表格得到 prependId="false"
than access the value like this
比访问这样的值
alert(jQuery('#someID').val());
if you don't want to add prependId="false"
to your form you will have to change the jquery selector from jQuery('#someID').val()
to jQuery("[id$='someID']").val()
如果您不想添加prependId="false"
到表单中,则必须将 jquery 选择器从 更改jQuery('#someID').val()
为jQuery("[id$='someID']").val()
EDIT
编辑
since your form called editlFrm
因为你的表格叫 editlFrm
try this (make sure to assign someIDid to your p:inputText
)
试试这个(确保将someIDid分配给您的p:inputText
)
alert(jQuery('#editlFrm\:someID').val());
回答by Robby Cornelissen
I came across this question when trying to do something similar, and discovered that PrimeFaces exposes a widget's JQuery element through the jq
property.
我在尝试做类似的事情时遇到了这个问题,发现 PrimeFaces 通过jq
属性公开了一个小部件的 JQuery 元素。
So in your example, you could simply do:
所以在你的例子中,你可以简单地做:
function loadPlayer() {
alert(youtlink.jq.val());
}
In more recent PrimeFaces versions (since 5.0), exported widgets seem to no longer pollute the global namespace, but need to be accessed using the PF()
method. The JQuery element can however still be accessed in the same way.
在最近的 PrimeFaces 版本中(自 5.0 起),导出的小部件似乎不再污染全局命名空间,但需要使用该PF()
方法进行访问。然而,仍然可以以相同的方式访问 JQuery 元素。
function loadPlayer() {
alert(PF('youtlink').jq.val());
}
回答by Michal.S
My I think the simplest way:
我的我认为最简单的方法:
PF('youtlink').jq.val();
You can set the value of input field like this:
您可以像这样设置输入字段的值:
PF('youtlink').jq.val('new value');
other way:
另一种方式:
PrimeFaces.widgets.youtlink.jq.val()
and another way if you have ID:
如果您有身,另一种方式是:
$(PrimeFaces.escapeClientId("yourFormID:youtlink")).val()
tested with PF 5.3
用 PF 5.3 测试
回答by Edson Cezar
Also, there a simple way with javascript:
此外,还有一种简单的 javascript 方法:
<h:form id="yourID" >
<p:inputText id="anyID" value="any text" widgetVar="youtlink" ></p:inputText>
</h:form>
function loadPlayer() {
alert(document.getElementById('yourID:anyID').value);
}