2013-08-16 110 views
-1

我有两个字段,其名称分别是:格式字段和扩展字段。在数组中搜索并匹配值

我要匹配像两个领域:

var format  = $("#someid").val();// Eg value is excel; 
var extension = $("#someid").val();// eg value is xls; 
var FormatCheck= {"excel":["xls","xlsx"],"word":["doc","docx"],"html"["htm","html"],"mpp":["mpp"],"pdf":["pdf"],"ppt":["ppt","pptx"]}; 

然后,我要比较thevalue格式和扩展字段与FormatCheck数组。

如果它完全匹配数组,那么我必须插入它。

+0

你确定'FormatCheck'是你想要它是什么?它看起来像JSON,你说它是一个数组,我会认为它不是...... – Joum

+0

没有其他我使用像FormatCheck一样的键,值对像Excel:xls,xlsx,因为它的值 –

+0

我不认为这是你有什么适合的目的... – Joum

回答

-1

你不需要jquery。你可以试试这个

var FormatCheck = "xls,xlsx,doc,docx,html,htm,html,mpp,pdf,ppt,pptx".indexOf($("#someid").val()) > -1; 

if(FormatCheck) { /*extension is in range*/} 
else { /*it is not*/} 
0

我希望这应该工作,

var format  = $("#format_id").val();// Eg value is excel; 
var extension = $("#ext_id").val();// eg value is xls; 
var FormatCheck= [excel["xls","xlsx"],word["doc","docx"],html["htm","html"],mpp["mpp"],pdf["pdf"],ppt["ppt","pptx"]]; //Double dimensional array, 

function validExtension(format, extension, FormatCheck){ 
    for(var i = 0 , len = FormatCheck.length; i < len; i++){ 
    if($.inArray(FormatCheck[format], extension) == 1) return true; //Check if the format exists. 
    } 
    return false; 
} 

validExtension("excel","xls", FormatCheck) //returns true. 

有人请帮助我,如果我错了地方纠正答案。

0

如果您FormatCheck举办不同它会更简单:

var FormatCheck = {"xls": "excel", "xlsx": "excel", ... } 

否则,您需要扫描整个表,找到扩展:

var type = null; 
$.each(FormatCheck, function(key,val) { 
    if ($.inArray(val, extension) >= 0) { 
     type = key; 
    } 
}); 
相关问题