html ajax c,CHtml::ajaxLink()

本文介绍如何利用CHtml::ajaxLink方法生成链接来发起AJAX请求,并展示了如何配置不同的选项如更新目标元素、加载指示器等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Generates a link that can initiate AJAX requests.

Syntax

public static string ajaxLink(string $text, mixed $url, array $ajaxOptions=array ( ), array $htmlOptions=array ( ))

Example

echo CHtml::ajaxLink(

$text = 'Click me',

$url = '/',

$ajaxOptions=array (

'type'=>'POST',

'dataType'=>'json',

'success'=>'function(html){ jQuery("#your_id").html(html); }'

),

$htmlOptions=array ()

);

Output:

Click me

jQuery('body').on('click', '#yt0', function () {

jQuery.ajax({

'type': 'POST',

'dataType': 'json',

'success': function (html) {

jQuery("#your_id").html(html);

},

'url': '/',

'cache': false,

'data': jQuery(this).parents("form").serialize()

});

return false;

});

});

Return values

string, the generated link

Examples

Example #1 : Ajax request using ajaxLink//In view:

echo CHtml::ajaxLink(

'Test request',          // the link body (it will NOT be HTML-encoded.)

array('ajax/reqTest01'), // the URL for the AJAX request. If empty, it is assumed to be the current URL.

array(

'update'=>'#req_res'

));

echo '

...
';

//In controllerpublic function actionReqTest01() {

echo date('H:i:s');    Yii::app()->end();}

Output:

jQuery('body').on('click', '#yt0', function () {

jQuery.ajax({

'url': '/ajax/reqTest01',

'cache': false,

'success': function (html) {

jQuery("#req_res").html(html)

}

});

return false;

});

Test request

...

Example #2 : Ajax request using ajaxLink with loading image//In view:echo CHtml::ajaxLink(

'Test request',          // the link body (it will NOT be HTML-encoded.)

array('ajax/reqTest01Loading'), // the URL for the AJAX request. If empty, it is assumed to be the current URL.

array(

'update'=>'#req_res_loading',

'beforeSend' => 'function() {

$("#maindiv").addClass("loading");

}',        'complete' => 'function() {

$("#maindiv").removeClass("loading");

}',

));

echo '

...
';

//In controller:public function actionReqTest01Loading() {

sleep(4);   // Sleep for 4 seconds just to demonstrate the Loading Image can be seen, for learning purpose only

echo date('H:i:s');    Yii::app()->end();}

Output:

jQuery('body').on('click', '#yt0', function () {

jQuery.ajax({

'beforeSend': function () {

$("#maindiv").addClass("loading");

},

'complete': function () {

$("#maindiv").removeClass("loading");

},

'url': '/ajax/reqTest01Loading',

'cache': false,

'success': function (html) {

jQuery("#req_res_loading").html(html)

}

});

return false;

});

});

Test request

...

References

http://www.yiiframework.com/doc/api/1.1/CHtml#ajaxLink-detail

http://www.yiiplayground.com/index.php?r=AjaxModule/ajax/ajaxRequest#ajaxLink

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值