で小さな請求書アプリケーションを書いていますYii
。私はdatabase
モデルのために持っていitem
ます。こんなitem database
感じです
+++++++++++++++++
| InvoiceItems |
+++++++++++++++++
+ id +
+ name +
+ description +
+ unit_cost +
+ quantity +
+ tax +
+ total +
+++++++++++++++++
今、item model
私はドロップダウンリストを作成しました。ここではunit_cost, quantity, tax
、Ajaxですべてのアイテム名とそのその他を取得しています。を行うためにtotal
、私は次のようなビジネスロジックを持っています:
unit_cost
乗算されますquantity
- その後、税金が合計に追加されます。
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
しているときはまったく機能していません。コンソールパネルに値が表示されていません。