jQuery获取地址栏参数插件(模仿C#)

jQuery获取地址栏参数插件(模仿C#),第1张

复制代码

代码如下:

$request

=

(function

()

{

var

apiMap

=

{};

function

request(queryStr)

{

var

api

=

{};

if

(apiMap[queryStr])

{

return

apiMap[queryStr];

}

apiqueryString

=

(function

()

{

var

urlParams

=

{};

var

e,

d

=

function

(s)

{

return

decodeURIComponent(sreplace(/\+/g,

"

"));

},

q

=

queryStrsubstring(queryStrindexOf('')

+

1),

r

=

/([^&=]+)=([^&])/g;

while

(e

=

rexec(q))

urlParams[d(e[1])]

=

d(e[2]);

return

urlParams;

})();

apigetUrl

=

function

()

{

var

url

=

queryStrsubstring(0,

queryStrindexOf('')

+

1);

for

(var

p

in

apiqueryString)

{

url

+=

p

+

'='

+

apiqueryString[p]

+

"&";

}

if

(urllastIndexOf('&')

==

urllength

-

1)

{

return

urlsubstring(0,

urllastIndexOf('&'));

}

return

url;

}

apiMap[queryStr]

=

api;

return

api;

}

$extend(request,

request(windowlocationhref));

return

request;

})();

接下来简介绍一下插件的用法。

我们可以通过

$requestqueryString["key"]

就能获取到地址栏上面key参数

大多数情况下我用这个插件来读取一些配置信息。

我们有时候会在

html

标签中加入一些其他的非HTML属性来作为标志信息,比如我们有时候会写<a

id="demo"

href="javascript:;"

config="title=hello&auto=true&">Demo</a>

这样子我们通过

var

config

=

$request($("#demo")attr("config"))queryString;

var

title

=

configtitle;

很方便就能读取a上面的配置信息,而且我们不需要给a

添加太多其他的非Html属性。

下面是我用jquery写的选中后立马判断选中的文件的类型,不是的话提示,然后清除,对应的可以自己改成纯js形式:

 //文件上传文件选择后事件

 $(document)ready(function() {

  $("input[id^='fileToUpload']")each(

//这里是用了each因为是多文件上传,input的id都是fileToUpload  开头

    function() {

 $("#" + $(this)attr("id") + "")live('change',function() {

 var fileName = $(this)val();

 if (fileName != null&& fileName != "") {

  //lastIndexOf如果没有搜索到则返回为-1

  if (fileNamelastIndexOf("") != -1) {

   var fileType = (fileNamesubstring(fileNamelastIndexOf("") + 1,

  fileNamelength))toLowerCase();

   var suppotFile = new Array();

   suppotFile[0] = "jpg";

   suppotFile[1] = "gif";

   suppotFile[2] = "bmp";

   suppotFile[3] = "png";

   suppotFile[4] = "jpeg";

   for ( var i = 0; i < suppotFilelength; i++) {

    if (suppotFile[i] == fileType) {

if (fileNamelength > 100) {

 alert("文件名长度不能超过100字符");

 if (!windowaddEventListener) {      

    documentgetElementById(fileName[j])outerHTML+='';  //IE清除inputfile

    }else { 

     documentgetElementById(fileName[j])value = "";   //FF清除inputfile

    } 

 return false;

}

return true;

 } else {

continue;

 }

 }

  alert("文件类型不合法,只支持 jpg、gif、png、jpeg类型!");

    if (!windowaddEventListener) {      

    documentgetElementById(fileName[j])outerHTML+='';  //IE

    }else { 

     documentgetElementById(fileName[j])value = "";   //FF

    } 

   return false;

  } else {

   alert("文件类型不合法,只支持 jpg、gif、png、jpeg类型!");

    if (!windowaddEventListener) {      

    documentgetElementById(fileName[j])outerHTML+='';  //IE

    }else { 

     documentgetElementById(fileName[j])value = "";   //FF

    } 

   return false;

  }

 }

});

});

});

我觉这是浏览器的事情了,只要你返回的数据是下载模式,然后将返回的字符串重新放入浏览器的url中,相当于:windowlocation="返回的字符串连接";

你试一试,应该可以。

这个有很多的方法:

具两个简单的例子,仅供参考:

1 var list='111,22,33,44,55,66';

   var a = listsplit(',');

   apop()

   alert(ajoin(','));

2 listreplace(/,66/,'')

最近因为项目需要研究了下bootstrap fileinput的使用,来记录下这几天的使用心得吧。

前台html页面的代码

<form role="form" id="importFile" method="post"

enctype="multipart/form-data">

<div class="row">

<div class="col-md-3" >

</div>

<div class="col-md-3 ">

<input type="radio" name="excelType" class="radio" id="line" value="line"><label for="line"> 参数2</label>

</div>

<div class="col-md-3 ">

<input type="radio" name="excelType" class="radio" id="pipeline" value="pipeline"><label for="pipeline"> 参数3</label>

</div>

<div class="col-md-3 ">

<input type="radio" name="excelType" class="radio" id="jdj" value="jdj"><label for="jdj"> 参数4</label>

</div>

</div>

<input id="excelFile" name="excelFile" class="file-loading"

type="file" multiple accept="xls,xlsx" data-min-file-count="1"

data-show-preview="true"> <br>

</form>

js进行插件的初始化和一些参数的设置

$("#excelFile")fileinput({

uploadUrl:"rest/import/importExcel",//上传的地址

uploadAsync: true,

language : "zh",//设置语言

showCaption: true,//是否显示标题

showUpload: true, //是否显示上传按钮

browseClass: "btn btn-primary", //按钮样式

allowedFileExtensions: ["xls", "xlsx"], //接收的文件后缀

maxFileCount: 10,//最大上传文件数限制

uploadAsync: true,

previewFileIcon: '<i class="glyphicon glyphicon-file"></i>',

allowedPreviewTypes: null,

previewFileIconSettings: {

'docx': '<i class="glyphicon glyphicon-file"></i>',

'xlsx': '<i class="glyphicon glyphicon-file"></i>',

'pptx': '<i class="glyphicon glyphicon-file"></i>',

'jpg': '<i class="glyphicon glyphicon-picture"></i>',

'pdf': '<i class="glyphicon glyphicon-file"></i>',

'zip': '<i class="glyphicon glyphicon-file"></i>',

},

uploadExtraData: function() {

var extraValue = null;

var radios = documentgetElementsByName('excelType');

for(var i=0;i<radioslength;i++){

if(radios[i]checked){

extraValue = radios[i]value;

}

}

return {"excelType": extraValue};

}

});

注意: uploadExtraData函数中只能用原生JS来取值,不能用JQuery来获取值,此参数用来向后台传递附加参数,以方便处理,最简单的写法是:

uploadExtraData: {

"excelType": documentgetElementByID('id')

}

<input type="radio" name="excelType" class="radio网络搜索引擎营销sem竞价培训班>

以上就是关于jQuery获取地址栏参数插件(模仿C#)全部的内容,包括:jQuery获取地址栏参数插件(模仿C#)、input file,javascript, jquery判断文件类型,在input中设置accept属性只是好看,求两种代码,方便大家、jquery 返回数据流,怎样d出下载框。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9397042.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存