如何将 JavaScript 值传递给 JSF EL 和支持 bean?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4696722/
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
How to pass JavaScript values to JSF EL and backing bean?
提问by Margus Pala
I am doing JSF geolocation service where I need to pass latitude and longitude to bean for processing. HTML5 allows getting location with JavaScript, for example like is done in http://code.google.com/p/geo-location-javascript/. Putting following code to JSF page shows alert with GPS coordinates
我正在做 JSF 地理定位服务,我需要将纬度和经度传递给 bean 进行处理。HTML5 允许使用 JavaScript 获取位置,例如在http://code.google.com/p/geo-location-javascript/ 中完成的操作。将以下代码放入 JSF 页面会显示带有 GPS 坐标的警报
<script>
if (geo_position_js.init()) {
geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true,options:5000});
} else {
alert("Functionality not available");
}
function success_callback(p) {
alert('lat='+p.coords.latitude.toFixed(2)+';lon='+p.coords.longitude.toFixed(2));
}
function error_callback(p) {
alert('error='+p.message);
}
</script>
How to use p.coords.latitude.toFixed(2)
value to pass it for example to h:inputtext
component?
如何使用p.coords.latitude.toFixed(2)
值将其传递给例如h:inputtext
组件?
回答by BalusC
You need to realize that JSF runs at webserver and produces a bunch of HTML/CSS/JS code which get sent from webserver to webbrowser and that the webbrowser only runs HTML/CSS/JS. Rightclick the page in webbrowser and choose View Source. In place of the <h:inputText>
you'll see something like
您需要意识到 JSF 在网络服务器上运行并生成一堆 HTML/CSS/JS 代码,这些代码从网络服务器发送到网络浏览器,而网络浏览器仅运行 HTML/CSS/JS。右键单击 webbrowser 中的页面并选择View Source。代替<h:inputText>
你会看到类似的东西
<input type="text" id="formid:inputid" />
In JS, you can easily grab HTML elements from the HTML DOM using document
functions and alter it.
在 JS 中,您可以使用document
函数轻松地从 HTML DOM 中获取 HTML 元素并对其进行更改。
var input = document.getElementById('formid:inputid');
input.value = 'new value';