当前位置: 代码迷 >> Ajax >> asp.net-Jquery传值的有关问题[贴]
  详细解决方案

asp.net-Jquery传值的有关问题[贴]

热度:529   发布时间:2012-03-23 12:06:21.0
asp.net---Jquery传值的问题[求助贴]
以上是前台代码:
C# code
<%@ Page Language="C#"AutoEventWireup="true"CodeBehind="Login.aspx.cs"Inherits="HISWEB.Login"%> 
<!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"> 
<meta http-equiv="Content-Type"content="text/html;charset=utf-8"> 
<title>系统登录</title> 
<link rel="stylesheet"href="css/layout.css"type="text/css"media="all"charset="utf-8"/> 
<link rel="stylesheet"href="css/validationEngine.jquery.css"type="text/css"/> 
<link rel="stylesheet"href="css/template.css"type="text/css"/> 

<script src="js/jquery-1.4.1.min.js"type="text/javascript"charset="utf-8"></script> 

<script src="js/jquery.infieldlabel.min.js"type="text/javascript"charset="utf-8"></script> 

<script src="js/jquery.validationEngine-cn.js"type="text/javascript"charset="utf-8"> 
</script> 

<script src="js/jquery.validationEngine.js"type="text/javascript"charset="utf-8"></script> 

<script type="text/javascript"charset="utf-8"> 
$(function() { $("label").inFieldLabels(); }); 
</script> 

<script type="text/javascript"> 
varis_login = false; 
jQuery(document).ready(function() { 
jQuery("#formID").validationEngine(); 
}); 

//无参数调用 
$(document).ready(function() { 
$('#btn').click(function() { 
$.ajax({ 
type: "POST", //访问WebService使用Post方式请求 
contentType: "application/json", //WebService 会返回Json类型 
url: "WebService.asmx/is_Login", //调用WebService的地址和方法名称组合 ---- WsURL/方法名 
data: "{ jzkh:"+ $('#jzkh').val() + ",pwd:"+ $('#pwd').val() + " }", //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到 
dataType: 'json', 
//async: false, //同步 async: false,默认 async: true 
success: function(result) { //回调函数,result,返回值 
is_login = result.d; 
if(is_login == true) { $('#t').append("登录成功!!!"); } 
else{ $('#t').append("登录失败!!!"); } 
} 
}); 
}); 
}); 
</script> 

<!--[iflte IE 6]> 
<style type="text/css"media="screen"> 
form label { 
background: #fff; 
} 
</style> 
<![endif]--> 
</head> 
<body> 
<h1> 
系统登录</h1> 
<form id="formID"runat="server"> 
<fieldset> 
<p> 
<label for="jzkh"> 
卡号</label><br /> 
<input type="text"name="jzkh"value=""id="jzkh"
class="validate[required,custom[onlyNumberSp],custom[onlyLetterNumber],maxSize[20]] text-input"> 
</p> 
<p> 
<label for="pwd"> 
密码</label><br /> 
<input type="password"name="pwd"value=""id="pwd"
class="validate[required,custom[onlyLetterNumber],maxSize[20]] text-input"> 
</p> 
</fieldset> 
<button type="button"id="btn"> 
登录</button> 
<p style="font-size: xx-small; text-align: right"> </p> 
<div id="t"> 
</div> 
</form> 
</body> 
</html> 




以下是后台代码:
C# code
using System; 
using System.Data; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 

namespace HISWEB 
{ 
/// <summary> 
/// WebService 的摘要说明 
/// </summary> 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
[System.Web.Script.Services.ScriptService] 
publicclassWebService : System.Web.Services.WebService 
{ 
/// <summary> 
/// 无参数 
/// </summary> 
/// <returns></returns> 
[WebMethod] 
publicString HelloWorld() 
{ 
return"HelloWorl"; 
} 

/// <summary> 
/// 是否登录成功 
/// </summary> 
/// <param name="jzkh">就诊卡号</param> 
/// <param name="pwd">密码</param> 
/// <returns></returns> 
[WebMethod] 
publicBoolean is_Login(string jzkh, string pwd) 
{ 
Boolean is_Login = false; 
if(jzkh == "0000"&& pwd == "0000") 
{ 
is_Login = true; 
} 
returnis_Login; 
} 
} 
} 



内容很简单。就是用户输入id和pwd,然后判断是否等于"0000",然后做相应处理。

问题的关键点在于这里。
C# code
data: "{ jzkh:"+ $('#jzkh').val() + ",pwd:"+ $('#pwd').val() + " }", 


如果换成:
C# code
data: "{ jzkh:'0000',pwd:'0000' }", 



是没有问题的。但是这样数据就写死了。最终目的是希望根据用户输入的动态数据来判断的。

望各位大侠帮忙指教。万分感谢~





------解决方案--------------------
data: "{ jzkh:'"+ $('#jzkh').val() + "',pwd:'"+ $('#pwd').val() + "' }", 
加上单引号

  相关解决方案