脚本:
var tempvalue=""; //文本框显示
var temp_key = "";
var all_str=""; //用于验证四则运算合法 将所有代码转化成数字
var key_array = new Array();
var value_array = new Array();
var row=0; //数组标记 增加时加1,撤销时减1
//选择计算器
function setFormula(num){
key_array.push(num);
value_array.push(num);
if(temp_value == ""){
temp_value = value_array[row];
}else{
temp_value += value_array[row];
}
if(temp_key == ""){
temp_key = key_array[row];
}else{
temp_key += key_array[row];
}
document.getElementById("accountDetailVO.formulaText").value = temp_value; //中文显示
document.getElementById("accountDetailVO.formula").value = temp_key; //保存到数据库
row++;
}
//选择工资项
function setFormulaItem(num){
var items = document.getElementById("accountDetailVO.item");
var itemName = "";
var item = "";
for(var i = 0; i < items.length; i++){
if(items[i].selected){
itemName = document.all.item.options[i].text;
item = document.all.item.options[i].value;
}
}
key_array.push(item);
value_array.push(itemName);
if(temp_value == ""){
temp_value = value_array[row];
}else{
temp_value += value_array[row];
}
if(temp_key == ""){
temp_key = key_array[row];
}else{
temp_key += key_array[row];
}
document.getElementById("accountDetailVO.formulaText").value = temp_value; //中文显示
document.getElementById("accountDetailVO.formula").value = temp_key; //保存到数据库
row++;
}
//撤销
function clk_cancel(){
if(row != 0){ //撤销时从数组中删除数据
var end_key = key_array.pop();
var end_value = value_array.pop();
temp_key = temp_key.substring(0,temp_key.lastIndexOf(end_key));
temp_value = temp_value.substring(0,temp_value.lastIndexOf(end_value));
document.getElementById("accountDetailVO.formula").value = temp_key; //保存到数据库
document.getElementById("accountDetailVO.formulaText").value = temp_value; //显示
row--;
}
}
function clk_delete(){
document.getElementById("accountDetailVO.formula").value = "";
document.getElementById("accountDetailVO.formulaText").value = "";
temp_value = "";
temp_key = "";
}
function clk_save(){
var parent_window = parent.dialogArguments;
var formula = parent_window.document.getElementById("accountDetailVO.formula");
var formulaText = parent_window.document.getElementById("accountDetailVO.formulaText");
var str=temp_key;
str = str.replace(/[ ]/g,"");
if(str == ""){
alert("表达式为空,请重新编辑");
return false;
}
if(/^(\(?[+-]?\(?\w+\.?\w*\)?[+-/\*]\(?[+-]?\(?\w+\.?\w*\)?)+$/.test(str)){
}else{
alert("表达式格式不正确,请修改");
return false;
}
if(formula != null){
formula.value = temp_key;
}
if(formulaText != null){
formulaText.value = temp_value;
}
window.close();
}
function clk_close(){
window.close();
}
function editForlula(){
var formula = document.getElementById("accountDetailVO.formula");
var formulaText = document.getElementById("accountDetailVO.formulaText");
temp_key = formula.value;
temp_value = formulaText.value;
}
-----------------------------------------------------------------页面-----------------------------------------------------------
<body οnlοad="editForlula()">
<form name="accountDetailForm" method="post" action="">
<html:hidden name="accountDetailForm" property="accountDetailVO.formula"/>
<table width="99%" border="0" cellspacing="1" cellpadding="0"
class="ReportTable" align="center">
<tr class="ButtonTableContent">
<td align="right">
<input name="fh" class="Button2_mouseout"
οnmοuseοver="this.className='Button1_mouseover'"
οnmοuseοut="this.className='Button2_mouseout'"
οnmοusedοwn="this.className='Button1_mousedown'"
οnmοuseup="this.className='Button1_mouseup'" type="button"
value="确 定" οnclick="javascript:clk_save();">
<input name="fh" class="Button2_mouseout"
οnmοuseοver="this.className='Button1_mouseover'"
οnmοuseοut="this.className='Button2_mouseout'"
οnmοusedοwn="this.className='Button1_mousedown'"
οnmοuseup="this.className='Button1_mouseup'" type="button"
value="清 除" οnclick="javascript:clk_delete();">
<input name="fh" class="Button2_mouseout"
οnmοuseοver="this.className='Button1_mouseover'"
οnmοuseοut="this.className='Button2_mouseout'"
οnmοusedοwn="this.className='Button1_mousedown'"
οnmοuseup="this.className='Button1_mouseup'" type="button"
value="撤 销" οnclick="javascript:clk_cancel();">
<input name="fh" class="Button2_mouseout"
οnmοuseοver="this.className='Button1_mouseover'"
οnmοuseοut="this.className='Button2_mouseout'"
οnmοusedοwn="this.className='Button1_mousedown'"
οnmοuseup="this.className='Button1_mouseup'" type="button"
value="关 闭" οnclick="javascript:clk_close();">
</td>
</tr>
</table>
<TABLE width="30%" border=2 width=50 height=60 cellpadding=1
cellspacing=5 align="center">
<tr>
<td height="172" colspan="5"><html:textarea name="accountDetailForm"
property="accountDetailVO.formulaText" rows="10" cols="60"
styleId="isLongerTah100:公式" readonly="true" /></td>
<td></td>
</tr>
<tr>
<td colspan="5">
工资项名称
</td>
</tr>
<tr>
<td width="250" rowspan="6"><bean:define
id="itemList" name="accountDetailForm"
property="itemList" /> <html:select name="accountDetailForm"
property="accountDetailVO.item" multiple="true" size="10"
styleId="item" style="width:200px" οndblclick="setFormulaItem(this.value)" >
<html:options collection="itemList" property="key"
labelProperty="value" />
</html:select>
</td>
<TD colspan="2" align="center" ><input name="btnClear" type="Button"
value=" ( " οnclick="setFormula('(')"></TD>
<TD colspan="2" align="center"><input name="btnClearEntry" type="Button"
value=" ) " οnclick="setFormula(')')"></TD>
</tr>
<TR>
<TD ><input name="btnSeven" type="Button" value=" 7 "
οnclick="setFormula('7')"></TD>
<TD><input name="btnEight" type="Button" value=" 8 "
οnclick="setFormula('8')"></TD>
<TD><input name="btnNine" type="Button" value=" 9 "
οnclick="setFormula('9')"></TD>
<TD><input name="btnNeg" type="Button" value=" + "
οnclick="setFormula('+')"></TD>
</TR>
<TR>
<TD ><input name="btnFour" type="Button" value=" 4 "
οnclick="setFormula('4')"></TD>
<TD><input name="btnFive" type="Button" value=" 5 "
οnclick="setFormula('5')"></TD>
<TD><input name="btnSix" type="Button" value=" 6 "
οnclick="setFormula('6')"></TD>
<TD align=middle><input name="btnPlus" type="Button"
value=" - " οnclick="setFormula('-')"></TD>
</TR>
<TR>
<TD ><input name="btnOne" type="Button" value=" 1 "
οnclick="setFormula('1')"></TD>
<TD><input name="btnTwo" type="Button" value=" 2 "
οnclick="setFormula('2')"></TD>
<TD><input name="btnThree" type="Button" value=" 3 "
οnclick="setFormula('3')"></TD>
<TD align=middle><input name="btnMultiply" type="Button"
value=" * " οnclick="setFormula('*')"></TD>
</TR>
<TR>
<TD colspan="2"><input name="btnZero" type="Button" value=" 0 "
οnclick="setFormula('0')"></TD>
<TD><input name="btnDecimal" type="Button" value=" . "
οnclick="setFormula('.')"></TD>
<TD><input name="btnDecimal" type="Button" value=" / "
οnclick="setFormula('/')"></TD>
</TR>
</TABLE>
</FORM>
</body>