Php 如何使用按键事件执行计算

Php 如何使用按键事件执行计算,php,javascript,jquery,yii,Php,Javascript,Jquery,Yii,这是我的创建页面 费率=劳动力+机械+材料+总场地+总办事处+利润 在提交按钮之前,单击我要完成上述计算并将其填入“费率”文本字段。是否有类似按键(如windows应用程序中)这样的事件来执行此操作? 我是yii的新手,我想你们能帮我做到这一点 这是我通过Yii生成的表单 <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'trboq-form',

这是我的创建页面

费率=劳动力+机械+材料+总场地+总办事处+利润

在提交按钮之前,单击我要完成上述计算并将其填入“费率”文本字段。是否有类似按键(如windows应用程序中)这样的事件来执行此操作? 我是yii的新手,我想你们能帮我做到这一点

这是我通过Yii生成的表单

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'trboq-form',
    'enableAjaxValidation'=>false,
)); ?>

<?php
    $projectWbs = Yii::app()->session['projectWbs'];
?>

<?php
    $subBoqNo=Yii::app()->session['$subBoqNo']  ;
    $subBoqDes=Yii::app()->session['subBoqDes']  ;
    $mainBoqDes=Yii::app()->session['mainBoqDes']  ;
?>




    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>


<!-- Start of First table-->
 <div class="form">
  <div class="group" style="width:600px; padding-left:50px;margin-top:40px; margin-left:15px">
   <fieldset>
      <legend>General</legend>
      <table>
     <tr>
       <th><?php echo $form->hiddenfield($model,'Project_Code'); ?></th>
       <th><?php echo $form->hiddenfield($model,'Project_Code',array('size'=>20,'value'=>$projectWbs,'readonly'=>true)); ?></th>
       <th><?php echo $form->error($model,'Project_Code'); ?></th>
    </tr>
    <tr>
        <th><?php echo $form->labelEx($model,'Sub_BOQ_No'); ?></th>
        <th><?php echo $form->textField($model,'Sub_BOQ_No',array('size'=>20,'value'=>$subBoqNo,'readonly'=>true)); ?></th>
        <th><?php echo $form->error($model,'Sub_BOQ_No'); ?></th>
    </tr>
       <tr>
        <th><?php echo $form->labelEx($model,'Sub_BOQ_Description'); ?></th>
        <th><?php echo $form->textField($model,'Sub_BOQ_Description',array('size'=>60,'maxlength'=>150, 'value'=>$subBoqDes,'readonly'=>true)); ?>        </th>
        <th><?php echo $form->error($model,'Sub_BOQ_Description'); ?></th>
    </tr>
      <tr>
        <th><?php echo $form->labelEx($model,'BOQ_Item_Code'); ?></th>
        <th><?php echo $form->textField($model,'BOQ_Item_Code',array('size'=>25,'maxlength'=>25)); ?></th>
        <th><?php echo $form->error($model,'BOQ_Item_Code'); ?></th>
    </tr>
       <tr>
        <th><?php echo $form->labelEx($model,'BOQ_Item_Name'); ?></th>
        <th><?php echo $form->textarea($model,'BOQ_Item_Name',array('rows'=>6, 'cols'=>40)); ?></th>
        <th><?php echo $form->error($model,'BOQ_Item_Name'); ?></th>
    </tr>
    </tr>
       <tr>
        <th><?php echo $form->labelEx($model,'BOQ_Description'); ?></th>
        <th><?php echo $form->textarea($model,'BOQ_Description',array('rows'=>6, 'cols'=>40)); ?></th>
        <th><?php echo $form->error($model,'BOQ_Description'); ?></th>
    </tr>

    <tr>
        <th><?php echo $form->labelEx($model,'Unit_Code'); ?></th>
        <th> <?php echo $form->dropdownlist($model,'Unit_Code',CHtml::listData(MaUnits::model()->findAll(),'Unit_Code','Unit_Code'),array('width'=>              '25', 'empty'=>'please select'));  ?></th>
        <th><?php echo $form->error($model,'Unit_Code');?></th>
    </tr>

     <tr>
        <th><?php echo $form->labelEx($model,'Qty'); ?></th>
        <th><?php echo $form->textField($model,'Qty'); ?></th>
        <th><?php echo $form->error($model,'Qty'); ?></th>
    </tr>
        </table>
    </fieldset>

  </div>
  </div>

  <div class="form">
  <div class="group" style="width:600px; padding-left:50px;margin-top:40px; margin-left:15px">
   <fieldset>
      <legend>Cost</legend>
      <table>
      <tr>
        <th><?php echo $form->labelEx($model,'Labour'); ?></th>
        <th><?php echo $form->textField($model,'Labour'); ?></th>
        <th><?php echo $form->error($model,'Labour'); ?></th>
    </tr>
    <tr>
        <th><?php echo $form->labelEx($model,'Machinery'); ?></th>
        <th><?php echo $form->textField($model,'Machinery'); ?></th>
        <th><?php echo $form->error($model,'Machinery'); ?></th>
    </tr>
       <tr>
        <th><?php echo $form->labelEx($model,'Matirial'); ?></th>
        <th><?php echo $form->textField($model,'Matirial'); ?></th>
        <th><?php echo $form->error($model,'Matirial'); ?></th>
    </tr>
      <tr>
        <th><?php echo $form->labelEx($model,'Side_over_head'); ?></th>
        <th><?php echo $form->textField($model,'Side_over_head'); ?></th>
        <th><?php echo $form->error($model,'Side_over_head'); ?></th>
    </tr>
       <tr>
        <th><?php echo $form->labelEx($model,'Head_office_over_head'); ?></th>
        <th><?php echo $form->textField($model,'Head_office_over_head'); ?></th>
        <th><?php echo $form->error($model,'Head_office_over_head'); ?></th>
    </tr>
     <tr>
        <th><?php echo $form->labelEx($model,'Profit'); ?></th>
        <th><?php echo $form->textField($model,'Profit'); ?></th>
        <th><?php echo $form->error($model,'Profit'); ?></th>
    </tr>
    <tr><th><p>----------------------------------------------------------------------------</p></th></tr>
    <tr>
        <th><?php echo $form->labelEx($model,'Rate'); ?></th>
        <th><?php echo $form->textField($model,'Rate'); ?></th>
        <th><?php echo $form->error($model,'Rate'); ?></th>
    </tr>
      <tr>
        <th><?php echo $form->labelEx($model,'Amount_Total'); ?></th>
        <th><?php echo $form->textField($model,'Amount_Total'); ?></th>
        <th><?php echo $form->error($model,'Amount_Total'); ?></th>
      </tr>
        </table>
    </fieldset>
  </div>
  </div>

        <div class="row buttons" style="margin-left:580px">
            <?php echo CHtml::submitButton($model->isNewRecord ? 'Save' : 'Save'); ?>
        </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

带*的字段是必需的

一般的 成本 ----------------------------------------------------------------------------


您可以使用javascript和jquery完成这类工作,看看它是如何工作的:

了解javascript需要一些工作,但您可以使用它构建强大的东西

在你的情况下,可能是这样的:

<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
        <form id="myForm">
            <input id="Labour" type="text"/> <label>Labour</label> <br/>
            <input id="Machinery" type="text"/> <label>Machinery</label> <br/>
            <input id="Material" type="text"/> <label>Material</label><br/>
            <input id="Site" type="text"/> <label>Site Over Head</label><br/>
            <input id="HeadOffice" type="text"/> <label>Head Office Over Head</label><br/>
            <input id="Profit" type="text"/> <label>Profit</label><br/>
            <input type="submit">  <br/>
        </form>

        <script type="text/javascript">
            $("#myForm input").each(function(){
                $(this).keyup(function(){
                    var labour = $("#Labour").val();
                    var Machinery = $("#Machinery").val();
                    var Material = $("#Material").val();
                    var SiteOverHead = $("#Site").val();
                    var HeadOfficeOverHead = $("#HeadOffice").val();
                    labour = $.isNumeric(labour)?labour:0;
                    Machinery = $.isNumeric(Machinery)?Machinery:0;
                    Material = $.isNumeric(Material)?Material:0;
                    SiteOverHead = $.isNumeric(SiteOverHead)?SiteOverHead:0;
                    HeadOfficeOverHead = $.isNumeric(HeadOfficeOverHead)?HeadOfficeOverHead:0;
                    $("#Profit").val(parseInt(labour)+parseInt(Machinery)+parseInt(Material)+parseInt(SiteOverHead)+parseInt(HeadOfficeOverHead));
                });
            });
        </script>
    </body>
</html>

劳工
机械
材料
头顶上的站点
总部在头顶上
利润

$(“#myForm输入”)。每个(函数(){ $(this).keyup(function(){ var-labour=$(“#labour”).val(); var机械=$(“#机械”).val(); var Material=$(“#Material”).val(); var siteoverage=$(“#Site”).val(); var HeadOfficeOverHead=$(“#总部”).val(); 人工=$.isNumeric(人工)?人工:0; 机械=美元。isNumeric(机械)?机械:0; 物料=$.isNumeric(物料)?物料:0; 站点开销=$.isNumeric(站点开销)?站点开销:0; HeadOfficeOverHead=$.isNumeric(HeadOfficeOverHead)?HeadOfficeOverHead:0; 美元(“#利润”).val(人力)+机械)+材料)+场地管理费)+总部管理费); }); });

祝你好运。

谢谢你的回复。但是在我的例子中,我想在单击submit按钮之前计算费率。当用户输入人工、机械等时。。。费率必须计算输入的上述值的相同时间。(如Ajax验证)这就是.change()事件的作用。但是如果你想让它发生在一把钥匙上,你应该看看这里:谢谢你的回复。我试过这个。它不起作用。我在上面附上了我的表格。