[英]Unable to replace div content with JavaScript in JSF
I use the JavaScript code below to replace contents of a div
, but it's not working. 我使用下面的JavaScript代码替换
div
内容,但是它不起作用。
<h:head>
<script type="text/javascript">
function Findchange() {
document.getElementById("myDiv").innerHTML='ravi';
}
</script>
</h:head>
<h:body>
<h:form id="form" onsubmit="Findchange();">
<div id="myDiv">hello</div>
<h:commandButton value="submit" ></h:commandButton>
</h:form>
</h:body>
The method getElementById
is defined on the document object. 方法
getElementById
在文档对象上定义。 So use it like this: 所以像这样使用它:
document.getElementById('myDiv').innerHTML = 'ravi';
Also you might need to return
false
so the page isn't submitted: 另外,您可能需要
return
false
以便不提交页面:
<script>
function Findchange(){
document.getElementById('myDiv').innerHTML = 'ravi';
return false;
}
</script>
<form onsubmit="return Findchange();">
<div id="myDiv">hello</div>
<input type="submit"/>
</form>
Otherwise any change you make will be immediately lost. 否则,您所做的任何更改都会立即丢失。
In fact, if you analyze code as is , you'll get the 'approximate' turn of events as: 实际上,如果按原样分析代码,则事件的“近似”轮次为:
div
is changed . div
值已更改 。 div
back to hello
. div
回到了hello
。 This is how it all works. 这就是全部的工作方式。
By the way, <div id="myDiv" onclick="Findchange()">hello</div>
is working, as well as not submitting a form with <h:commandButton>
by employing its onclick
attribute, like in: 顺便说一句,
<div id="myDiv" onclick="Findchange()">hello</div>
起作用了,并且没有通过使用其<div id="myDiv" onclick="Findchange()">hello</div>
<h:commandButton>
的onclick
属性来提交表单,例如:
<h:commandButton value="submit" onclick="return Findchange()"/>
with 与
function Findchange() {
document.getElementById("myDiv").innerHTML='ravi';
return false;
}
As a side note, the JSF way to achieve your functionality is to update the contents of the div with AJAX (or instead synchronously) like: 附带说明一下,实现功能的JSF方法是使用AJAX(或同步)更新div的内容,例如:
<h:form id="form">
<h:panelGroup id="div-id" display="block">
#{bean.name}
</h:panelGroup>
<h:commandButton value="Submit" action="#{bean.changeName}">
<f:ajax render="div-id"/>
</h:commandButton>
</h:form>
with bean 用豆
@ManagedBean
@RequestScoped
public class Bean {
private String name = "Josh";//getter + setter
public String changeName() {
name = "Ravi";
return null;
}
}
You should not submit the form to do this. 您不应该提交表格来做到这一点。 If you submit for it will get lost and page will refresh again.
如果您提交,它将丢失,页面将再次刷新。
You have invoke the JavaScript function at button level as shown below and button should NOT submit the form as written in the code below, 您已经在按钮级别调用了JavaScript函数,如下所示,按钮不应提交以下代码中编写的表单,
<h:commandButton value="submit" type="button" onclick="Findchange()"></h:commandButton>
Inspired from @louisbros: 灵感来自@louisbros:
<h:head>
<script>
function Findchange(){
document.getElementById('myDiv').innerHTML = 'ravi';
return false;
}
</script>
</h:head>
<h:form onsubmit="return Findchange();">
<div id="myDiv">hello</div>
<h:commandButton value="test" />
</h:form>
Instead of old submit use <f:ajax
with execute="@form" render="@form
(for example) And call your js code on success
of the <f:ajax
代替旧的提交,使用
<f:ajax
和execute="@form" render="@form
(例如),并在<f:ajax
success
时调用您的js代码
<h:head>
<script type="text/javascript">
function Findchange() {
document.getElementById("myDiv").innerHTML='ravi';
}
</script>
</h:head>
<h:body>
<h:form id="form">
<div id="myDiv">hello</div>
<h:commandButton value="submit" >
<f:ajax execute="@form" render="@form" onevent="function(data) { if (data.status === 'success') {Findchange(); } }"></f:ajax>
</h:commandButton>
</h:form>
</h:body>
u should write " form:myDiv " not only " myDiv " like that 你应该这样写“ form:myDiv ”而不是“ myDiv ”
<h:head>
<script type="text/javascript">
function Findchange() {
document.getElementById("form:myDiv").innerHTML='ravi';
}
</script>
</h:head>
<h:body>
<h:form id="form" onsubmit="Findchange();">
<div id="myDiv">hello</div>
<h:commandButton value="submit" ></h:commandButton>
</h:form>
</h:body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.