Last active
December 17, 2015 18:49
-
-
Save yushiro/5655603 to your computer and use it in GitHub Desktop.
jquery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
取被选中的input元素 | |
$('input[name=radioName]:checked', '#myForm').val() | |
#myForm是表示socpe, 提高jquery的查找效率 | |
serializeArray():序列化表格元素 (类似 '.serialize()' 方法) 返回 JSON 数据结构数据。 | |
Java 代码 | |
注意,此方法返回的是JSON对象而非JSON字符串。需要使用插件或者第三方库进行字符串化操作。 | |
jquery.json插件: | |
http://code.google.com/p/jquery-json/ | |
$("form").serializeArray() | |
执行结果: | |
[{"name": "single", "value": "Single"}, {"name": "param", "value": "Multiple"}, {"name": "param", "value": "Multiple3"}, {"name": "check", "value": "check2"}, {"name": "radio", "value": "radio1"}] | |
serializeArray的中文提交处理。 | |
暂时未找到更直接的处理方式, 提交的只能遍历,对指定的一些KEY进行编码处理 | |
var obj = $('#frm1').serializeArray(); | |
for(var item in obj){ | |
if(obj[item].name == "txtName"){ | |
obj[item].value = escape(obj[item].value); | |
break; | |
} | |
} | |
最终用ajax提交数据到服务器时候需要用的.serialize()方法, 把数据转换成 | |
"txtName=%E4%B8%AD%E6%96%87&txtMobile=12345678901&selDate=1&selTime=1" | |
这样的格式 | |
自动从服务器端获取数据,并生成dropdownlist | |
$.ajax({ | |
type: 'GET', | |
url: 'webhandler.ashx', | |
data: 'meth=radio1', | |
success: function (data) { | |
var obj = data;//$.parseJSON(data); | |
$(obj).each(function (id) { | |
$('#selDate').append("<option " + (id == 0 ? "selected=\"selected\"" : "") + " value='" + this.Value + "'>" + this.Name + "</option >"); | |
}); | |
} | |
}); | |
---------------------------------------------------------------------------------------------------------------------- | |
取Radio值的方法 | |
$("input[name='aaa']:checked").val() | |
$.param()方法是serialize()方法的核心,用来对一个数组或对象按照key/value进行序列化。 | |
比如将一个普通的对象序列化: | |
var obj = {a:1,b:2,c:3}; | |
var k = $.param(obj); | |
alert(k); //输出a=1&b=2&c=3 | |
去除ASP.NET动态FORM生成的一些系统input | |
var o = $(document.forms[0]).serializeArray(); | |
var bb=[];for(var item in o){if(o[item].name.indexOf('__') == -1) bb.push(o[item]);} | |
var postdata = $.param(bb); | |
移除select的全部option | |
$('#mySelect') | |
.empty() | |
.append('<option selected="selected" value="whatever">text</option>') | |
; | |
---------------------------------------------------------------------------------------------------------------------- | |
jquery下调用focus()聚焦方法 | |
参考:http://blog.miniasp.com/post/2009/10/29/The-six-versions-that-let-DOM-focus-using-jQuery.aspx | |
$('#Account').setfocus(); | |
(function($) | |
{ | |
jQuery.fn.setfocus = function() | |
{ | |
return this.each(function() | |
{ | |
var dom = this; | |
setTimeout(function() | |
{ | |
try { dom.focus(); } catch (e) { } | |
}, 0); | |
}); | |
}; | |
})(jQuery); | |
---------------------------------------------------------------------------------------------------------------------- | |
jquery下正确调用trim的方法 | |
var content = $('#content').val(); | |
$.trim(content) | |
不要调用content.trim(), 这个不是jquery的方法,是浏览器的自带方法, 某些浏览器不支持 | |
---------------------------------------------------------------------------------------------------------------------- | |
取多个class的元素 | |
eg. <button class="btn btn-large btn-danger" type="button">Large button</button> | |
$('.btn-large.btn-danger.btn') | |
来获取, 与class的书写顺序无关 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment