2

で小さな請求書アプリケーションを書いていますYii。私はdatabaseモデルのために持っていitemます。こんなitem database感じです

  +++++++++++++++++
  |  InvoiceItems |
  +++++++++++++++++
  +      id       +
  +      name     +
  +  description  +
  +    unit_cost  +
  +    quantity   +
  +    tax        +
  +    total      +
  +++++++++++++++++

今、item model私はドロップダウンリストを作成しました。ここではunit_cost, quantity, tax、Ajaxですべてのアイテム名とそのその他を取得しています。を行うためにtotal、私は次のようなビジネスロジックを持っています:

  1. unit_cost乗算されますquantity
  2. その後、税金が合計に追加されます。
  3. total合計は、最終的にファイルのフィールドにポップアップ表示され_form.phpます。

計算のために、私はこのjQueryスクリプトを書きました:

<script type="text/javascript">
jQuery(document).ready(function(){
  jQuery("#InvoiceItems_unit_cost, #InvoiceItems_quantity, #InvoiceItems_tax").on('keyup',function(event){
    var subtotal = jQuery("#InvoiceItems_quantity").val() * jQuery("#InvoiceItems_unit_cost").val();
    var total = subtotal + (subtotal  * jQuery("#InvoiceItems_tax").val() / 100);
    jQuery("#InvoiceItems_total").val(total);
  });
});
</script>

手動で値を挿入している場合、これは正常に機能していますが、値がデータベースから取得されている場合は機能しません。私は完全に立ち往生しています。

アップデート

のようにFirefoxのコンソールタブでajaxを介してデータベースからフェッチした後に値を表示しようとすると、console.log("#InvoiceItems_quantity").val()が表示されan empty stringます。jsonの形式でデータベースからデータをフェッチするAjaxコード

        <?php echo $form->dropDownList($customers,'customer_name',CMap::mergeArray(CHtml::listData(Customers::model()->findAll(), 'customer_name',       'customer_name'
        ),
        array(
        'empty'=>array('Select'=>'- - Choose One - -'),
        'id'=>'Customers_name',
        'ajax'=> array(
            'type'=>'GET',
            'id'=>'abc',
            'url'=>$this->createUrl('Invoices/customerdetails'),// action that will generate the data
            'data'=>'js:"customer_name="+$(this).val()',// this is the data that we are sending to the action in the controller
            'dataType'=>'json',// type of data we expect back from the server
            'success'=>'js:updateFieldsCustomers',// a javascript function that will execute when the request completes
            'beforeSend'=>'js:function(){
                if($("#Customers_name").val() == "create_customer_link") {
                    addCustomers();
                    $("#dialogCustomers").dialog("open");
                    return false;
                }
            }',
        ),
        'options'=>array(
            'create_customer_link' => array('class'=>'create-link'),
        )
    )
);?>

   <?php
  Yii::app()->clientScript->registerScript('update','
  function updateFieldsCustomers(data, textStatus, jqXHR){
  // select each input field by id, and update its value
  $("#InvoiceItems_unit_cost").val(data.unit_cost);
  $("#InvoiceItems_quantity").val(data.quantity);
  $("#InvoiceItems_discount").val(data.discount);
  // similarly update the fields for the other inputs
  }
');
  ?>

最近の更新

試してみるとconsole.log("#InvoiceItems_quantity").val()、変更後のそのフィールドの実際の値が表示されていることがわかりました。1つの値を選択すると、コンソールパネルに前に入力した値の値が表示されていることを意味します。on change関数を使用したので、これはすべて機能していると思いますが、使用.selectしているときはまったく機能していません。コンソールパネルに値が表示されていません。

4

2 に答える 2

1

私はこの質問の解決策を得ました。このようになるはずです

<script type="text/javascript">
jQuery(document).ready(function(){
  jQuery("#Product_name").ajaxComplete(function(event,request,settings){
    var subtotal = jQuery("#InvoiceItems_quantity").val() * jQuery("#InvoiceItems_unit_cost").val();
    var total = subtotal + (subtotal  * jQuery("#InvoiceItems_tax").val() / 100);
    jQuery("#InvoiceItems_total").val(total);
  });
});
</script>
于 2012-10-01T13:49:12.947 に答える
0

これを試して

    <script type="text/javascript">
    jQuery(document).ready(function(){
      jQuery("#Items_unit_cost, #Items_quantity, #Items_discount").on('keyup',calculate_total);

     function calculate_total(){
           var subtotal = jQuery("#Items_quantity").val() * jQuery("#Items_unit_cost").val();
           var total = subtotal + (subtotal  * jQuery("#Items_discount").val() / 100);
           jQuery("#Items_total").val(total);
         }

calculate_total();

    });

    </script>
于 2012-09-17T06:40:13.787 に答える