jQuery1.4下载、性能及新特性详解

jQuery1.4正式版发布了,官方同步推出了The 14 Days of jQuery 网站,来介绍jQuery1.4的新特性和教程,其中有一篇文章详细介绍了jQuery1.4的新增功能,现转载其中文翻译如下:

原文:jQuery 1.4 Released

翻译:coolnalu

jQuery 1.4 发布啦

为了庆祝jQuery的四周岁生日, jQuery的团队荣幸的发布了jQuery Javascript库的最新主要版本! 这个版本包含了大量的编程,测试,和记录文档的工作,我们为此感到很骄傲。

我要以个人的名义感谢 Brandon Aaron, Ben Alman, Louis-Rémi Babe, Ariel Flesler, Paul Irish, Robert Kati?, Yehuda Katz, Dave Methvin, Justin Meyer, Karl Swedberg, and Aaron Quint。谢谢他们在修复BUG和完成这次发布上所做的工作。

下载(Downloading)

按照惯例,我们提供了两份jQuery的拷贝,一份是最小化的(我们现在采用Google Closure作为默认的压缩工具了),一份是未压缩的(供纠错或阅读)。
阅读全文 »

jQuery对表单元素的取值和赋值操作

今天在使用jQuery的过程中,遇到两个不大不小的问题,写出来分享一下。

jQuery读取input元素的值:

<input type="text" id="keyword" />

使用常规的思路:$(“#keyword”).value 取值是取不到的,因为此时$(‘#keydord’)已经不是个element,而是个jquery对象,所以应该使用:

var inputValue = $("#keyword").val();

因为Qjuery对象中第一个元素即为DOM对象,所以也可以这样取值:

var inputValue = $("#keyword")[0].value;

jQuery中 val()函数的作用:

val()
获得第一个匹配元素的当前值。
在 jQuery 1.2 中,可以返回任意元素的值。包括select。如果多选,将返回一个数组,其包含所选的值。

返回值
String,Array

同理,对input元素赋值,需要:

$("#keyword").val("");

或:

$("#keyword")[0].value = "";

附:jquery 1.3.2对基本表单元素的取值方法


 /*获得TEXT.AREATEXT的值*/
var textval = $("#text_id").attr("value");
//或者
var textval = $("#text_id").val();

/*获取单选按钮的值*/
var valradio = $("input[type=radio]:checked").val();

/*获取一组名为(items)的radio被选中项的值*/
var item = $('input[name=items]:checked').val();

/*获取复选框的值*/
var checkboxval = $("#checkbox_id").attr("value");

/*获取下拉列表的值*/
var selectval = $('#select_id').val();

/*文本框,文本区域*/
$("#text_id").attr("value",");//清空内容
$("#text_id").attr("value",'test');//填充内容

/*多选框checkbox*/
$("#chk_id").attr("checked",");//使其未勾选
$("#chk_id").attr("checked",true);//勾选
if($("#chk_id").attr('checked')==true) //判断是否已经选中

/*单选组radio*/
$("input[type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项

/*下拉框select*/
$("#select_id").attr("value",'test');//设置value=test的项目为当前选中项
$("testtest2").appendTo("#select_id")//添加下拉框的option
$("#select_id").empty();//清空下拉框

/*获取一组名为(items)的radio被选中项的值*/
var item = $('input[name=items]:checked').val(); //若未被选中 则val() = undefined

/*获取select被选中项的文本*/
var item = $("select[name=items] option:selected").text();

/*select下拉框的第二个元素为当前选中值*/
$('#select_id')[0].selectedIndex = 1;

/*radio单选组的第二个元素为当前选中值*/
$('input[name=items]').get(1).checked = true;

/*重置表单*/
$("form").each(function(){
.reset();
});