Demo中的js总结
对页面数据的绑定读取:
var liveStatus = $("#liveStatus").val();//读取隐藏字段的值
var perssional = $("#professional").val();
var marry = $("#marriage").val();
var lives;
var StringUtils ={//字符串的一些方法
IsNullOrEmpty:function(v){
return !(typeof(v)==="string"&&v.length!=0);
},
Trim:function(v){
return v.replace(/^\s+|\s+$/g, "")
},
ValidStringLength:function(str){//对一个长的字符串按照中文逗号和英文的逗号分隔,并检查子字符串的长度不能超过8位
if(!StringUtils.IsNullOrEmpty(str) && str.length <= 85)
{
var isCanPost = 1;
var hoppys;
var hoppyses;
var k = 0;
hoppys = str.split(",");
for(k=0;k<hoppys.length;k++)
{
var j = 0;
hoppyses = hoppys[k].split(",");
for(j=0;j<hoppyses.length;j++)
{
var len = hoppyses[j].length;
if(len>8)
{
isCanPost = -1;
}
}
}
if(isCanPost == -1)
{
return -1;
}
else
{
return 1;
}
}
}
};
$(
function(){
if(!StringUtils.IsNullOrEmpty(marry))//对radioButton的绑定和操作
{
$("input[name='r2']").each(function() {
this.checked = this.value == marry;
});
}
if(!StringUtils.IsNullOrEmpty(perssional))
{
$("input[name='r1']").each(function() {
this.checked = this.value == perssional;
});
}
if(!StringUtils.IsNullOrEmpty(liveStatus))//对CheckBox的绑定和操作
{
lives = liveStatus.split(",");
var len = lives.length;
var i = 0;
for(i=0;i<len;i++)
{
$("input[name='ck_LiveStatus']").each(
function(k,item) {
if(item.value == lives[i])
{
item.checked = true;
}
//this.checked = this.value == lives[i];因为里面有i变量不可以用这一句话
}
);
}
}
//对checkBox的点击操作
$("input[name='ck_LiveStatus']").click(
function()
{
var the_value = this.value+',';
if(this.checked == true)
{
liveStatus = liveStatus + the_value;
}
else
{
liveStatus = liveStatus.replace(the_value,'');
}
}
);
//对radioBotton的点击操作
$("input[name='r1']").click(
function()
{
perssional = this.value
}
);
$("input[name='r2']").click(
function()
{
marry = this.value
}
);
}
);
提交的代码:在aspx中的调用:href="javascript:personalExtend.update();"
var personalExtend={
update:function()
{
if(StringUtils.IsNullOrEmpty($("#blog1").val()))
{
$("#blog1").val("http://");
}
if(StringUtils.IsNullOrEmpty($("#blog2").val()))
{
$("#blog2").val("http://");
}
if(StringUtils.IsNullOrEmpty($("#blog3").val()))
{
$("#blog3").val("http://");
}
var blogs=$("#blog1").val()+","+$("#blog2").val()+","+$("#blog3").val();
var hobbies=$("#hobbies").val();
var lovingStar=$("#lovingStar").val();
var lovingBrand=$("#lovingBrand").val();
var region=$("#region").val();
var regionName=$("#region option:selected")[0].innerHTML;//取select中的text值
var city=$("#city").val();
var cityName=$("#city option:selected")[0].innerHTML;
var homeadress = region+","+regionName+","+city+","+cityName;
if(StringUtils.IsNullOrEmpty(liveStatus))
{
alert(demoMessage.liveStatus);
return;
}
if(StringUtils.IsNullOrEmpty(perssional))
{
alert(demoMessage.perssional);
return;
}
if(StringUtils.IsNullOrEmpty(marry))
{
alert(demoMessage.marry);
return;
}
if(StringUtils.IsNullOrEmpty(blogs))
{
alert(demoMessage.blogs);
return;
}
if(StringUtils.IsNullOrEmpty(hobbies) || hobbies.length > 85)
{
alert(demoMessage.hobbies);
return;
}
if(StringUtils.ValidStringLength(hobbies)==-1)
{
alert(demoMessage.hobbies);
return;
}
if(StringUtils.IsNullOrEmpty(lovingStar) || lovingStar.length > 85)
{
alert(demoMessage.lovingStar);
return;
}
if(StringUtils.ValidStringLength(lovingStar)==-1)
{
alert(demoMessage.lovingStar);
return;
}
if(StringUtils.IsNullOrEmpty(lovingBrand) || lovingBrand.length > 85)
{
alert(demoMessage.lovingBrand);
return;
}
if(StringUtils.ValidStringLength(lovingBrand)==-1)
{
alert(demoMessage.lovingBrand);
return;
}
var personalExtendInfo = {
LiveStatus : liveStatus,
Professional : perssional,
Marriage : marry,
BlogAddresses : blogs,
Hobby : hobbies,
LovingBrand : lovingBrand,
LovingStar : lovingStar,
HomeAddress : homeadress
}
var strExtendInfo = $Json.ToJson(personalExtendInfo);
$.ajax({
type: "post",
dataType: "json",
url: $.newegg.buildCurrent("Ajax/Customer/AjaxPersonalExtendInfo.aspx"),
timeout: 30000,
data: {
StrExtendInfo:escape(strExtendInfo)
},
success: function(data, textStatus) {
alert("Success!");
},
complete: function(XMLHttpRequest, textStatus) {
},
error: function() {
}
});
}
};