我有以下代码,这会创建我在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并希望将输入的数据保存到数据库中,任何想法我怎样才能实现这一点。
谢谢, 阿希奈
答案 0 :(得分:1)
使用支持bean比使用Javascript更好地解决这个问题。使用ajax,您不需要任何页面刷新。有点像这样:
<强> 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;
}
}
注意强>
如果您想坚持使用keyup事件,您也可以轻松地将其绑定到<h:inputText value="#{bean.noOfFields}" />
。虽然我建议不要这样做,因为每次按键都会调用另一个ajax调用。