-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"]};

然后我必须将 Format 和 Extension 字段的值与FormatCheck.

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

4

3 回答 3

0

如果您的 FormatCheck 组织方式不同,它会更简单:

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

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

var type = null;
$.each(FormatCheck, function(key,val) {
    if ($.inArray(val, extension) >= 0) {
        type = key;
    }
});
于 2013-08-16T07:50:19.740 回答
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.
于 2013-08-16T07:48:07.713 回答
-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*/}
于 2013-08-16T07:38:00.007 回答