博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery-ajax之4:无刷新 select 数据源及事件绑定(2)
阅读量:4919 次
发布时间:2019-06-11

本文共 3571 字,大约阅读时间需要 11 分钟。

续前一篇 ,本篇讲解select的onchange事件和利用ajax静态调用后台方法
 
页面(asp.net)
页面(UseJason.aspx)
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeBehind
=
"
UseJason.aspx.cs
"
Inherits
=
"
AjaxTest.UseJason
"
%>
<!
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
runat
="server"
>
<
title
></
title
>
<
script
type
="text/javascript"
src
="Scripts/jquery-1.4.1.js"
></
script
>
<
script
type
="text/javascript"
>
$(document).ready(
function
() {
$(
"
#btnGetCars
"
).click(
function
() {
$.ajax({
type:
"
POST
"
,
contentType:
"
application/json; charset=utf-8
"
,
url:
"
UseJason.aspx/GetCars
"
,
data:
"
{}
"
,
dataType:
"
json
"
,
success: GetCars,
error:
function
errorCallback(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown
+
"
:
"
+
textStatus);
}
});
});
//
响应select的change事件
$(
"
#SelectCars
"
).change(
function
() {
CarSelectChange();
});
});
//
绑定selece的数据源
function
GetCars(result) {
$(result.d).each(
function
() {
var
o
=
document.createElement(
"
option
"
);
o.value
=
this
[
'
carName
'
];
o.text
=
this
[
'
carDescription
'
];
$(
"
#SelectCars
"
)[
0
].options.add(o);
});
}
//
通过select的change事件,调用后台方法:GetCarPrice
function
CarSelectChange() {
var
carName
=
$(
"
#SelectCars
"
)[
0
].value;
$.ajax({
type:
"
POST
"
,
contentType:
"
application/json; charset=utf-8
"
,
url:
"
UseJason.aspx/GetCarPrice
"
,
data:
"
{'carName':'
"
+
carName
+
"
'}
"
,
dataType:
"
json
"
,
success: ShowCarPrice,
error:
function
errorCallback(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown
+
"
:
"
+
textStatus);
}
});
}
//
把从后台反馈的结果给text赋值
function
ShowCarPrice(result) {
$(
"
#txtPrice
"
).val(result.d);
}
</
script
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
div
>
<
input
type
="button"
id
="btnGetCars"
value
="GetCars"
/>
<
div
style
="margin-top: 20px"
>
<
p
>
Cars list :
</
p
>
<
select
id
="SelectCars"
style
="width: 185px;"
>
</
select
>
<
p
>
Price :
</
p
>
<
input
type
="text"
id
="txtPrice"
/>
</
div
>
</
div
>
</
div
>
</
form
>
</
body
>
</
html
>
 
后台代码
后台代码(UseJason.aspx.cs)
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.Services;
namespace
AjaxTest
{
public
partial
class
UseJason : System.Web.UI.Page
{
private
static
List
<
Car
>
listCars;
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
[WebMethod]
public
static
List
<
Car
>
GetCars()
{
listCars
=
new
List
<
Car
>
() {
new
Car(
"
A1
"
,
"
A1 Description
"
,
205000
),
new
Car(
"
B12
"
,
"
B12 Description
"
,
105300
),
new
Car(
"
Dfe
"
,
"
Dfe Description
"
,
658982
),
new
Car(
"
Yfee
"
,
"
Yfee Description
"
,
8458700
),
new
Car(
"
UUdde
"
,
"
UUdde Description
"
,
6548225
)};
return
listCars;
}
[WebMethod]
public
static
string
GetCarPrice(
string
carName)
{
if
(
null
!=
listCars)
{
foreach
(Car car
in
listCars)
{
if
(car.carName
==
carName)
{
return
car.carPrice.ToString();
}
}
}
return
string
.Empty;
}
}
}
 
一个类文件
class(Car.cs)
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
namespace
AjaxTest
{
public
class
Car
{
public
string
carName {
get
;
set
; }
public
string
carDescription {
get
;
set
; }
public
double
carPrice {
get
;
set
; }
public
Car(
string
name,
string
description,
double
price)
{
this
.carName
=
name;
this
.carDescription
=
description;
this
.carPrice
=
price;
}
}
}
 
如有问题,请留言……

转载于:https://www.cnblogs.com/alvinyue/archive/2011/05/04/2036300.html

你可能感兴趣的文章
noip2010引水入城解题报告
查看>>
android开发:按钮事件
查看>>
C#数据库连接代码
查看>>
Mybatis的@Options注解
查看>>
Ubuntu12.04 下安装Chrome浏览器
查看>>
LeetCode:数据库技术【175-178】
查看>>
基于EasyUi的datagrid合并单元格JS写法
查看>>
修改input:file样式
查看>>
【linux日志】【日志分析】linux系统各日志文件的含义
查看>>
12.ARM伪指令操作
查看>>
linux相关命令及配置(三)
查看>>
Linux 命令-1
查看>>
算法学习(十三)
查看>>
【python3】文件格式转换windows转为unix
查看>>
Scala主构造器参数是否升级为成员与是否有get/set
查看>>
poj 3233 Matrix Power Series---矩阵快速幂
查看>>
hdu 4627 水数学题
查看>>
初见线段树
查看>>
"Storage Virtualization" VS "Software-Defined Storage"
查看>>
本周个人进步要点20160904
查看>>