Created
July 24, 2013 03:09
-
-
Save markyun/6067806 to your computer and use it in GitHub Desktop.
各种取值
This file contains hidden or 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
根据name取值: | |
$("input[name='mobile']").val() | |
根据id取值: | |
$("#mobile_reg_form").html() | |
根据name取值了遍历: | |
$("input[name='mobile']").each( | |
function(){ | |
alert($(this).val()); | |
} | |
) | |
取出form中的input: | |
<script type="text/javascript" language="JavaScript" charset="UTF-8"> | |
$(document).ready(function(){ | |
var a=$("form input"); | |
$.each( a, | |
function(name,object){ | |
alert(name+":"+$(object).val()); | |
} | |
); | |
}); | |
</script> | |
得到值(多个的情况): | |
$("input[name='mobile']")[0].value | |
$("input[name='mobile']").get(1).value | |
-----jquery添加删除样式-------- | |
给一个标签添加样式: | |
$("#id").addClass("style"); | |
删除一个标签的样式: | |
$("#id").removeClass("style"); | |
注:"#id" id是对应标签的id,style是对应css样式的名称 | |
获取选中的值 | |
获取一组radio被选中项的值 | |
var item = $('input[@name=items][@checked]').val(); | |
获取select被选中项的文本 | |
var item = $("select[@name=items] option[@selected]").text(); | |
select下拉框的第二个元素为当前选中值 | |
$('#select_id')[0].selectedIndex = 1; | |
radio单选组的第二个元素为当前选中值 | |
$('input[@name=items]').get(1).checked = true; | |
获取值: | |
文本框,文本区域: | |
$("#txt").attr("value"); | |
$("#txt").val(); | |
多选框checkbox: | |
$("#checkbox_id").attr("value"); | |
单选组radio: | |
$("input[@type=radio][@checked]").val(); | |
下拉框select: | |
$('#sel').val(); | |
控制表单元素: | |
文本框,文本区域: | |
$("#txt").attr("value",'');//清空内容 | |
$("#txt").attr("value",'11');//填充内容 | |
多选框checkbox: | |
$("#chk1").attr("checked",'');//不打勾 | |
$("#chk2").attr("checked",true);//打勾 | |
if($("#chk1").attr('checked')==undefined) //判断是否已经打勾 | |
单选组radio: | |
$("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项 | |
下拉框select: | |
$("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项 | |
$("<optionvalue='1'>1111</option><optionvalue='2'>2222</option>").appendTo("#sel")//添加下拉框的option | |
$("#sel").empty();//清空下拉框 | |
===================== | |
在Jquery中,用$("#id")来获得页面的input元素,其相当于document.getElementById("element")但是,该获取的是一个Jquery对象,而不是一个dom element对象.value是dom element对象的属性.所以,使用$("#id").value不能取到值取值的方法如下: | |
取值: | |
val = $("#id")[0].value; | |
$("#id")[0].value = "new value"; | |
赋值: | |
$("#id")[0].value = "new value"; | |
或者$("#id").val("new value"); | |
val = $("#id").attr("value"); | |
单选组radio: $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项 | |
下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项 | |
$("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//添加下拉框的option | |
$("#sel").empty();//清空下拉 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment