Jquery 局部刷新及 表单取值赋值 处理返回json数据 一些基本操作
/*获得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的项目为当前选中项
$("<option value='test'>test</option><option value='test2'>test2</option>").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();
});
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的项目为当前选中项
$("<option value='test'>test</option><option value='test2'>test2</option>").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();
});
添加一般处理程序:ajaxAllMsg.ashx
<%@ WebHandler Language="C#" Class="ajaxAllMsg" %>
using System;
using System.Web;
using System.Text;
public class ajaxAllMsg : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "text/xml";
// 返回值
//string temp = "\r\n{\"conversion\":{\r\n\"decimal\":12}}";
////DataSet ds = new DataSet("AccountList");
////ds = GetList("Account", "AccountId", "Loginname,Name", 50, 1, false, false, "1=1");
//context.Response.ContentType = "text/xml";
//context.Response.Charset = "GB2312";
//context.Response.Clear();
//context.Response.Write("<?xml version=\"1.0\" encoding=\"gbk\"?>\n " + ds.GetXml());
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.Append("<Account><loginname>中国人</loginname><name2>sdfert5</name2>");
sb.Append("</Account>");
context.Response.Write(sb.ToString());
//context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
using System;
using System.Web;
using System.Text;
public class ajaxAllMsg : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "text/xml";
// 返回值
//string temp = "\r\n{\"conversion\":{\r\n\"decimal\":12}}";
////DataSet ds = new DataSet("AccountList");
////ds = GetList("Account", "AccountId", "Loginname,Name", 50, 1, false, false, "1=1");
//context.Response.ContentType = "text/xml";
//context.Response.Charset = "GB2312";
//context.Response.Clear();
//context.Response.Write("<?xml version=\"1.0\" encoding=\"gbk\"?>\n " + ds.GetXml());
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.Append("<Account><loginname>中国人</loginname><name2>sdfert5</name2>");
sb.Append("</Account>");
context.Response.Write(sb.ToString());
//context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
调用页:DeJQuery.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DeJQuery.aspx.cs" Inherits="DeJQuery" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Ajax 留言板</title>
<script type="text/javascript" src="js/jquery-1.2.6.js">
</script>
<script type="text/javascript">
$(document).ready(function()
{
GetAllMsg();
});
function GetAllMsg(){
$("#AllMsg").html(" <img src='images/loading.gif'/>");
$.ajax({
type:'POST',
url:"ajaxAllMsg.ashx?act=getAll",
dataType:'xml', //默认["xml"/"html"] 返回数据类型:["xml" / "html" / "script" / "json" / "jsonp"]
data:"",
success:function(result)
{
// alert(result);
$(result).find('Account').each(function() {
var loginname = $(this).find("loginname").text();
var name3 = $(this).find("name2").text();
// $("#AllMsg").append("<li>" + loginname + " - " + name + "</li>");
// alert(name3);
// alert(loginname);
$("#AllMsg").html("<li>" + $(this).find("loginname").text() + " - " + $(this).find("name2").text() + "</li>");
});
////解析服务器端传来的数据
//for(key in result) $("#childsort").append("<option value="+result[key]+">"+result[key]+" </option>");
// var array=eval(data);
// $.each(array,function(i,r){
// var row=$("#row").clone();
// row.find("#col1").text(r.tid);
// row.find("#col2").text(r.state);
// row.find("#col3").text(r.project);
// row.appendTo("#testtable");
//objString = $(this).text(objString.substring(0,num) + "");
//if(msg == 0){
//$("#value_user").html("<img src='html/check_ok.gif'>");
//return 1;
//}else{
//$("#value_user").html("Sorry,该账号已经被注册,请换其他账号!");
//return 0;
//}
// $("#AllMsg").append("<li>id:"+json.EmployeeId+"|Name:"+json.EmployeeName+"|年龄:"+json.EmployeeInfo[0]+"|身高:"+json.EmployeeInfo[1]+"|体重:"+json.EmployeeInfo[2]+"</li>");
// $("#TextBox1").attr("value",'sdsd');//清空内容
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="AllMsg">
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Ajax 留言板</title>
<script type="text/javascript" src="js/jquery-1.2.6.js">
</script>
<script type="text/javascript">
$(document).ready(function()
{
GetAllMsg();
});
function GetAllMsg(){
$("#AllMsg").html(" <img src='images/loading.gif'/>");
$.ajax({
type:'POST',
url:"ajaxAllMsg.ashx?act=getAll",
dataType:'xml', //默认["xml"/"html"] 返回数据类型:["xml" / "html" / "script" / "json" / "jsonp"]
data:"",
success:function(result)
{
// alert(result);
$(result).find('Account').each(function() {
var loginname = $(this).find("loginname").text();
var name3 = $(this).find("name2").text();
// $("#AllMsg").append("<li>" + loginname + " - " + name + "</li>");
// alert(name3);
// alert(loginname);
$("#AllMsg").html("<li>" + $(this).find("loginname").text() + " - " + $(this).find("name2").text() + "</li>");
});
////解析服务器端传来的数据
//for(key in result) $("#childsort").append("<option value="+result[key]+">"+result[key]+" </option>");
// var array=eval(data);
// $.each(array,function(i,r){
// var row=$("#row").clone();
// row.find("#col1").text(r.tid);
// row.find("#col2").text(r.state);
// row.find("#col3").text(r.project);
// row.appendTo("#testtable");
//objString = $(this).text(objString.substring(0,num) + "");
//if(msg == 0){
//$("#value_user").html("<img src='html/check_ok.gif'>");
//return 1;
//}else{
//$("#value_user").html("Sorry,该账号已经被注册,请换其他账号!");
//return 0;
//}
// $("#AllMsg").append("<li>id:"+json.EmployeeId+"|Name:"+json.EmployeeName+"|年龄:"+json.EmployeeInfo[0]+"|身高:"+json.EmployeeInfo[1]+"|体重:"+json.EmployeeInfo[2]+"</li>");
// $("#TextBox1").attr("value",'sdsd');//清空内容
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="AllMsg">
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
<script src="https://files.cnblogs.com/aflyfly/google.js" type="text/javascript"></script>