最近将一个 perl 应用程序转换为 cakephp 2.3。现在我被困在一个使用 javascript 执行一些计算的页面上。我把javascript拿出来放在一个单独的文件中,但我也直接在视图中尝试过。
有 6 个表格,每个表格有 5 个字段和一个计算按钮。每个表格第一列中的字段(milkconc 和 matdose)是用户输入的。一旦用户点击退出,它应该执行计算并将它们显示在第二列字段中。单击计算时,将在第 5 个字段中显示结果。
当我从第一列字段中跳出时,我的 javascript 控制台显示:'Uncaught TypeError: Cannot read property 'milkconc' of undefined' 并且单击计算按钮会为相关字段提供类似的错误。我确定我需要更改 javascript 中的某些内容以适应 cakephp 的 forminput。我只是不知道是什么。在我的 default.ctp 中,我拥有echo $this->Html->script('tid');
具有所有功能的文件。
我认为会发生的是 javascript 会将值放在适当的字段中,一旦单击保存,cakephp 就会将值 javascript 放在那里并将其放入数据库中。
编辑视图:
<?php
echo $this->Html->css('style');
echo $this->Html->script('tabs');
echo $this->Html->script('tid');
?>
<?php echo $this->Form->create('Drug'); ?>
<i><b>These fields are currently under construction.</b></i>
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td><b>Adult Dose: </b></td>
</tr>
<tr>
<td>
<?php
echo $this->Form->input('adultdose', array(
'type' => 'text',
'name' => 'adultdose',
'size' => '24',
'maxlength' => '250',
'label'=> false,
//'class'=>'entryField'
));
?> </td>
</tr>
<tr>
<td><b>RID Calculations:
<script type="text/javascript">
//Previously tried the javascript here.
</script>
</b></td>
</tr>
<tr>
<td>
<table>
<tr>
<td>
<table class="border">
<tr>
<td class="title"> </td>
<td class="title" align="center" nowrap><b>Dose</b></td>
<td class="title" align="center" nowrap><b>mg/kg/day</b></td>
<td class="title"> </td>
<td class="title" align="center" nowrap><b>RID</b></td>
<td class="title"> </td>
</tr>
<tr>
<td nowrap>Milk Dose (ug/L)</td>
<td align="center">
<?php echo $this->Form->input('milkconc', array(
'type' => 'text',
'name' => 'milkconc',
'label' => false,
'size' => '7',
'maxlength' => '125',
'onblur' => 'milk_amount_calc(9);'
));
?>
</td>
<td align="center">
<?php echo $this->Form->input('theor_infa', array(
'type' => 'text',
'name' => 'theor_infa',
'label' => false,
'size' => '7',
'maxlength' => '125',
//'onblur' => 'milk_amount_calc(\'9\');'
));
?>
</td>
<td nowrap>mg/kg/day</td>
<td align="center">
<?php echo $this->Form->input('rid', array(
'type' => 'text',
'name' => 'rid',
'label' => false,
'size' => '7',
'maxlength' => '250',
//'onblur' => 'milk_amount_calc(\'9\');'
));
?>
</td>
<td><button name="buttonName" value="" type="button" onclick="calculate_rid('9');">Calculate</button></td>
</tr>
<tr>
<td>Mother's Dose (mg)</td>
<td align="center">
<?php echo $this->Form->input('matdose', array(
'type' => 'text',
'name' => 'matdose',
'label' => false,
'size' => '7',
'maxlength' => '250',
//'onblur' => 'mom_amount_calc(\'9\');'
));
?>
</td>
<td align="center">
<?php echo $this->Form->input('mdkg', array(
'type' => 'text',
'name' => 'mdkg',
'label' => false,
'size' => '7',
'maxlength' => '250',
//'onblur' => 'milk_amount_calc(\'9\');'
));
?>
</td>
<td>mg/kg/day</td>
<td>Reference...</td>
<td><!-- --></td>
</tr>
</table>
</td>
/* Code removed to save size. Basically the code above times 5 and each next field is fieldname1,2,3, etc */
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<td class="actionTable"><?php echo $this->Form->submit('Save/Update & Previous', array('name'=>'Previous')); ?></td>
<td class="actionTable"><?php echo $this->Form->submit('Save',array('style'=>'width:150px;')); ?></td>
<td class="actionTable"><?php echo $this->Form->submit('Save/Update & Next', array('name'=>'Next')); ?>
<?php echo $this->Form->end(); ?>
Javascript:
function milk_amount_calc(set) {
if (set == 1) {
var tmp = (document.form.milkconc1.value / 1000) *.15;
var val = roundNumber(tmp, 4);
document.form.theor_infa1.value = val;
}
else if (set == 2) {
var tmp = (document.form.milkconc2.value / 1000) *.15;
var val = roundNumber(tmp, 4);
document.form.theor_infa2.value = val;
}
else if (set == 3) {
var tmp = (document.form.milkconc3.value / 1000) *.15;
var val = roundNumber(tmp, 4);
document.form.theor_infa3.value = val;
}
else if (set == 4) {
var tmp = (document.form.milkconc4.value / 1000) *.15;
var val = roundNumber(tmp, 4);
document.form.theor_infa4.value = val;
}
else if (set == 5) {
var tmp = (document.form.milkconc5.value / 1000) *.15;
var val = roundNumber(tmp, 4);
document.form.theor_infa5.value = val;
}
else if (set == 9) {
var tmp = (document.form.milkconc.value / 1000) *.15;
var val = roundNumber(tmp, 4);
document.form.theor_infa.value = val;
}
}
function mom_amount_calc(set) {
if (set == 1) {
var tmp = document.form.matdose1.value /70;
var val = roundNumber(tmp, 4);
document.form.mdkg1.value = val;
}
else if (set == 2) {
var tmp = document.form.matdose2.value /70;
var val = roundNumber(tmp, 4);
document.form.mdkg2.value = val;
}
else if (set == 3) {
var tmp = document.form.matdose3.value /70;
var val = roundNumber(tmp, 4);
document.form.mdkg3.value = val;
}
else if (set == 4) {
var tmp = document.form.matdose4.value /70;
var val = roundNumber(tmp, 4);
document.form.mdkg4.value = val;
}
else if (set == 5) {
var tmp = document.form.matdose5.value /70;
var val = roundNumber(tmp, 4);
document.form.mdkg5.value = val;
}
else if (set == 9) {
var tmp = document.form.matdose.value /70;
var val = roundNumber(tmp, 4);
document.form.mdkg.value = val;
}
}
var set_0 = 0;
var set_1 = 0;
var set_2 = 0;
var set_3 = 0;
var set_4 = 0;
var set_5 = 0;
var low = 0;
var high = 0;
function calculate_rid(set) {
if (set == 1) {
if (document.form.theor_infa1.value == 0 || document.form.mdkg1.value == 0) {
alert("Milk Dose and Mother's Dose are Required\nCan't Calculate the Relative Infant Dose(RID)");
return;
}
set_1 = (document.form.theor_infa1.value / document.form.mdkg1.value) * 100;
set_1 = roundNumber(set_1, 4);
var val = set_1 + '%';
document.form.rid1.value = val;
}
else if (set == 2) {
if (document.form.theor_infa2.value == 0 || document.form.mdkg2.value == 0) {
alert("Milk Dose and Mother's Dose are Required\nCan't Calculate the Relative Infant Dose(RID)");
return;
}
set_2 = (document.form.theor_infa2.value / document.form.mdkg2.value) * 100;
set_2 = roundNumber(set_2, 4);
var val = set_2 + '%';
document.form.rid2.value = val;
}
else if (set == 3) {
if (document.form.theor_infa3.value == 0 || document.form.mdkg3.value == 0) {
alert("Milk Dose and Mother's Dose are Required\nCan't Calculate the Relative Infant Dose(RID)");
return;
}
set_3 = (document.form.theor_infa3.value / document.form.mdkg3.value) * 100;
set_3 = roundNumber(set_3, 4);
var val = set_3 + '%';
document.form.rid3.value = val;
}
else if (set == 4) {
if (document.form.theor_infa4.value == 0 || document.form.mdkg4.value == 0) {
alert("Milk Dose and Mother's Dose are Required\nCan't Calculate the Relative Infant Dose(RID)");
return;
}
set_4 = (document.form.theor_infa4.value / document.form.mdkg4.value) * 100;
set_4 = roundNumber(set_4, 4);
var val = set_4 + '%';
document.form.rid4.value = val;
}
else if (set == 5) {
if (document.form.theor_infa5.value == 0 || document.form.mdkg5.value == 0) {
alert("Milk Dose and Mother's Dose are Required\nCan't Calculate the Relative Infant Dose(RID)");
return;
}
set_5 = (document.form.theor_infa5.value / document.form.mdkg5.value) * 100;
set_5 = roundNumber(set_5, 4);
var val = set_5 + '%';
document.form.rid5.value = val;
}
else if (set == 9) {
if (document.form.theor_infa.value == 0 || document.form.mdkg.value == 0) {
alert("Milk Dose and Mother's Dose are Required\nCan't Calculate the Relative Infant Dose(RID)");
return;
}
set_0 = (document.form.theor_infa.value / document.form.mdkg.value) * 100;
set_0 = roundNumber(set_0, 4);
var val = set_0 + '%';
document.form.rid.value = val;
}
set_rid_range();
}
function set_rid_range() {
var arr = new Array();
var rng = new Array();
arr[0] = replacer(document.form.rid.value);
arr[1] = replacer(document.form.rid1.value);
arr[2] = replacer(document.form.rid2.value);
arr[3] = replacer(document.form.rid3.value);
arr[4] = replacer(document.form.rid4.value);
arr[5] = replacer(document.form.rid5.value);
arr = arr.sort(sortNumber);
var this_total = 0;
for(a = 0; a < arr.length; a++) {
this_total = this_total + parseFloat(arr[a]);
if (arr[a] > 0) {
rng.push(arr[a]);
}
}
if (this_total == 0) {
document.form.rid_range.value = '';
return;
}
rng = rng.sort(sortNumber);
var low = rng[0];
var high = rng.pop();
if ((low == high) && (low == 0)) {
document.form.rid_range.value = '';
return;
}
if (low == high) {
document.form.rid_range.value = high + '%';
}
else {
document.form.rid_range.value = low + '% - ' + high + '%';
}
}
function sortNumber(a,b) {
return a - b;
}
function replacer(d) {
return d.replace('%','');
}
function roundNumber(rnum, rlength) {
// Arguments: number to round, number of decimal places
var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
return newnumber;
}