我在codeigniter中的助手函数中定义了一个函数,当val和货币id传递给它时,它会返回格式化的价格。
if(!function_exists('format_price')){
function format_price($val,$curency_id=NULL){
$CI =& get_instance();
$CI->load->model('currency_model');
if ($curency_id) {
$Result=$CI->currency_model->getcurrency($curency_id);
$dec_place=round($Result['decimal_place']);
$value=number_format((float)$val,$dec_place,'.',',');
return $Result['symbol_left'].$value ." ".$Result['symbol_right'];
}
else{
$Result=$CI->currency_model->getDefaultCurrency();
$dec_place=round($Result['decimal_place']);
$value=number_format((float)$val,$dec_place,'.',',');
return $Result['symbol_left'].$value ." ".$Result['symbol_right'];
}
}
}
我需要的是通过javascript代码中的ajax来调用这个函数。这在没有控制器的情况下可能吗?还是我必须制作一个控制器?
您需要通过控制器发出请求,然后通过该控制器调用该函数,类似于:
$("#id").change(function()
{
$.ajax({
type: "POST",
url: base_url + "controller_name/your_function",
data: {val: $("#your_val").val(),currency_id: $("#your_cur").val()},
dataType: "JSON",
cache:false,
success:
function(data){
$("#your_elem").val(data.price);
}
});
然后在您的控制器上:
public function yourfunction()
{
$data = $this->input->post();
$price = format_price($data['val'],$data['currency_id']);
echo json_encode(array('price' => $price));
}
不要像这样使用ajax。。。
<script type="text/javascript">
$(document).ready(function(){
(function(){
<?php if(helper_function($variable)) { ?>
now your jquery script..........
<?php } ?>
});
});
</script>
根据需要自定义以上代码。。。。