问题描述
我有以下代码,这创建了我在textField内部输入的文本字段的数量.
i have the following code, this creates the number of textfields that i enter inside the textField.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Dashboard | BlueWhale Admin</title>
<link rel="stylesheet" type="text/css" href="../css/reset.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/text.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/grid.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/layout.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../css/nav.css" media="screen" />
<script type="text/javascript">
//<![CDATA[
function BuildFormFields($amount)
{
var
$container = document.getElementById('FormFields'),
$item, $field, $i;
$container.innerHTML = '';
for ($i = 0; $i < $amount; $i++) {
$item = document.createElement('div');
$item.style.margin = '3px';
$field = document.createElement('span');
$field.innerHTML = '<hr>'+'News '+ ($i+1) + " ";
$field.style.marginRight = '10px';
$item.appendChild($field);
$field = document.createElement('input');
$field.name = 'Design[' + $i + ']';
$field.id = 'Design[' + $i + ']';
$field.type = 'text';
$field.className = 'error';
$field.style.width = '500px';
$item.appendChild($field);
$container.appendChild($item);
}
}
function saveNews()
{
var value=document.getElementById("noOfNews").value;
if(value==="")
{
alert("Please Enter Some Data");
}
else
{
var $no = parseInt(value),$i;
for($i=0;$i < $no; $i++ )
{
var value1=document.getElementById( 'Design[' + $i + ']' ).value;
if(value1==="")
{
alert("Please enter Data");
break;
}
}
if($no===$i)
{
alert("Data Saved !!!");
}
}
}
//]]>
</script>
</head>
<body>
Number Of News Item To be Displayed <input id="noOfNews" type="text" onkeyup="BuildFormFields(parseInt(this.value, 10));" />
<div id="FormFields" style="margin: 20px 0px;"></div>
<button class="btn btn-blue" onclick="saveNews()">Save</button>
</body>
</html>
我正在使用jsf 2.0,并且想要将输入的数据保存到数据库中,任何想法我该如何实现.
I am using jsf 2.0 and want to save the entered data into the database, any ideas how can i achieve this.
谢谢,Abhinay
Thanks,Abhinay
推荐答案
与使用Javascript相比,使用后备bean可以更好地解决这一问题.使用ajax,您不需要刷新任何页面.与此类似:
You can better approach this with a backing bean than using Javascript. Using ajax you wouldn't need any page refreshes. Something along the lines of this:
HTML
<h:form>
<p>
<h:inputText value="#{bean.noOfFields}" />
<h:commandButton value="Create fields">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
</p>
<hr />
<p>
<c:forEach items=#{bean.values} varStatus="counter">
Field no. #{counter.index} <h:inputText value="#{bean.values[counter.index}" /><br />
</c:forEach>
<h:commandButton action="#{bean.submit}" value="Save" />
</p>
</h:form>
Bean.java
@ManagedBean
@ViewScoped
public class Bean {
private String noOfFields = 1;
private String[] values = new String[1];
public void submit() {
// save values in database
}
public String getNoOfFields() {
return noOfFields;
}
public void setNoOfFields(String noOfFields) {
try {
values = new String[Integer.valueOf(noOfFields)];
this.noOfFields = noOfFields;
catch(NumberFormatException ex) {
values = new String[1];
noOfFields = "1";
}
}
public String[] getValues() {
return values;
}
}
注意
如果要坚持键入事件,也可以轻松地将其绑定到<h:inputText value="#{bean.noOfFields}" />
.尽管我建议不要这样做,因为每次击键都会调用另一个ajax调用.
In case you want to stick to a keyup event, you can easily bind this to <h:inputText value="#{bean.noOfFields}" />
too. Though I'd recommend not doing this, since every keystroke will invoke another ajax call.
这篇关于将javascript创建的动态文本字段绑定到bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!