使用jQuery的表单元素AJAX微调器附件

Yesterday I showed you how to attach an AJAX spinner next to a form element using my beloved MooTools. Today I'll show you how to implement that same functionality using jQuery.

昨天我向您展示了如何使用心爱的MooTools在表单元素旁边附加AJAX微调器 。 今天,我将向您展示如何使用jQuery实现相同的功能。

XHTML (The XHTML)


<select class="ajax">
	<option value="">-- Select a Site--</option>
	<option value="David Walsh Blog">David Walsh Blog</option>
	<option value="Script & Style">Script & Style</option>
	<option value="Band Website Template">Band Website Template</option>
</select>

<br /><br />

<input type="text" id="my-text" class="ajax" />


Elements with the "ajax" CSS class will be our target.

具有“ ajax” CSS类的元素将是我们的目标。

jQuery JavaScript (The jQuery JavaScript)


$(document).ready(function() {
	//create image
	$('<img src="move-spinner.gif" id="spinner" />').css('position','absolute').hide().appendTo('body');
	//for every field change
	$('.ajax').change(function() {
		//get element position
		var position = $(this).offset();
		//position image
		$('#spinner').css({ top: position.top , left: position.left + $(this).width() + 30 }).fadeIn();
		//ajax
		$.post('<?php echo $_SERVER['REQUEST_URI']; ?>',{
			ajax:1,
			value: $(this).val()
		},function() {
			$('#spinner').fadeOut();
		});
	});
});


We inject the spinner image into the page and reposition it depending on which field is doing the request. Very simple!

我们将微调器图像注入到页面中,并根据哪个字段在执行请求来重新定位它。 很简单!

Isn't JavaScript fun? Oh yeah...and it makes the user experience better too.

JavaScript不好玩吗? 哦,是的,这也使用户体验也更好。

翻译自: https://davidwalsh.name/ajax-spinner-jquery

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值