我从Primefaces 5.2迁移到6.1。但是,我在inputNumber中使用widgetVar来解决javascript的操作。但是,widgetVar在6.1版本中不适用于inputNumber。
例:
头
<h:head>
<title>TODO supply a title</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<h:outputScript name="jquery/jquery.js" library="primefaces" />
<h:outputScript name="jquery/jquery-plugins.js" library="primefaces" />
<h:outputScript name="core.js" library="primefaces" />
<h:outputScript name="components.js" library="primefaces" />
</h:head>
<p:inputNumber thousandSeparator="." decimalSeparator="," widgetVar="wInput"
onchange="valueForInput('wInput')"/>
<script>
function valueForInput(wInput){
var inputNumber = PF(wInput);
console.log(inputNumber.getValue());
}
</script>
控制台错误
Uncaught TypeError: Cannot read property 'getValue' of undefined
at valueForInput (http://localhost:8084/sgwapp/:15:40)
at HTMLInputElement.onchange (http://localhost:8084/sgwapp/:11:2197)
at Object.trigger (http://localhost:8084/sgwapp/javax.faces.resource/jquery/jquery.js.xhtml?ln=primefaces&v=6.1:4585:27)
at HTMLInputElement.<anonymous> (http://localhost:8084/sgwapp/javax.faces.resource/jquery/jquery.js.xhtml?ln=primefaces&v=6.1:5289:17)
at Function.each (http://localhost:8084/sgwapp/javax.faces.resource/jquery/jquery.js.xhtml?ln=primefaces&v=6.1:384:23)
at jQuery.fn.init.each (http://localhost:8084/sgwapp/javax.faces.resource/jquery/jquery.js.xhtml?ln=primefaces&v=6.1:136:17)
at jQuery.fn.init.trigger (http://localhost:8084/sgwapp/javax.faces.resource/jquery/jquery.js.xhtml?ln=primefaces&v=6.1:5288:15)
at jQuery.fn.init.jQuery.fn.(anonymous function) [as change] (http://localhost:8084/sgwapp/javax.faces.resource/jquery/jquery.js.xhtml?ln=primefaces&v=6.1:8481:9)
at c.setValueToHiddenInput (http://localhost:8084/sgwapp/javax.faces.resource/inputnumber/inputnumber.js.xhtml?ln=primefaces&v=6.1:3:1714)
at copyValueToHiddenInput (http://localhost:8084/sgwapp/javax.faces.resource/inputnumber/inputnumber.js.xhtml?ln=primefaces&v=6.1:3:1542)
valueForInput @ (index):15
onchange @ (index):11
trigger @ jquery.js.xhtml?ln=primefaces&v=6.1:4585
(anonymous) @ jquery.js.xhtml?ln=primefaces&v=6.1:5289
each @ jquery.js.xhtml?ln=primefaces&v=6.1:384
each @ jquery.js.xhtml?ln=primefaces&v=6.1:136
trigger @ jquery.js.xhtml?ln=primefaces&v=6.1:5288
jQuery.fn.(anonymous function) @ jquery.js.xhtml?ln=primefaces&v=6.1:8481
setValueToHiddenInput @ inputnumber.js.xhtml?ln=primefaces&v=6.1:3
copyValueToHiddenInput @ inputnumber.js.xhtml?ln=primefaces&v=6.1:3
init @ inputnumber.js.xhtml?ln=primefaces&v=6.1:3
e.(anonymous function) @ core.js.xhtml?ln=primefaces&v=6.1:5
c @ core.js.xhtml?ln=primefaces&v=6.1:5
createWidget @ core.js.xhtml?ln=primefaces&v=6.1:1
cw @ core.js.xhtml?ln=primefaces&v=6.1:1
(anonymous) @ (index):11
fire @ jquery.js.xhtml?ln=primefaces&v=6.1:3148
fireWith @ jquery.js.xhtml?ln=primefaces&v=6.1:3260
ready @ jquery.js.xhtml?ln=primefaces&v=6.1:3472
completed @ jquery.js.xhtml?ln=primefaces&v=6.1:3503
最佳答案
您可以在堆栈中看到在构建小部件期间触发了onchange侦听器,这就是为什么无法使用PF
函数找到它的原因-它尚未准备好。
无论如何,您可能对初始值不感兴趣,因此请跳过此调用:
function valueForInput(wInput){
if (PF(wInput)) {
var inputNumber = PF(wInput);
console.log(inputNumber.getValue());
}
}