2013-04-26 72 views
0

我的表单使用查询字符串中的值获取预先填充的onload。在查询中,当国家=澳大利亚时,选择“澳大利亚”。但是,当国家=澳大利亚或国家=澳大利亚时,什么都不会被选中。使选择值不区分大小写/具有多个值

我的选择字段:

<select id="country" name="country" style="border: 1px solid rgb(204, 204, 204); font-family: Arial,Helvetica,sans-serif; font-size: 9pt; width: 150px;"> 
    <option value="" selected="selected">Please select country</option> 
    <option value="Australia">Australia</option> 
    <option value="Austria">Austria</option> 
</select> 

我想<option value="Australia, AUSTRALIA, australia">会使其工作。我如何完成这项工作?这是否(Can an Option in a Select tag carry multiple values?)是一个选项?

否则,我怎么能使选择字段大小写不敏感的前置查询字符串值?

JS的查询字符串添加:

function populate(form) { 
    if (location.search == null || location.search.length < 1) return; // no querystring 
    var pairs = location.search.substring(1).split("+"); 
    for (var p = 0; p < pairs.length; ++p) { 
     var pair = pairs[p].split("="); 
     var name = pair[0]; 
     var value = unescape(pair[1].replace(/\+/g, " ")); 
     var fld = form.elements[name]; 
     var ftype = null; 
     var farray = false; 
     var atype = Array; 
     if (fld != null) { 
      if (fld.length != null && fld.length >= 1 && fld[0].type != null && fld[0].type != undefined) { 
       ftype = fld[0].type; 
       farray = true; 
      } else { 
       ftype = fld.type; 
      } 
     } 
     switch (ftype) { 
     case "text": 
     case "hidden": 
     case "textarea": 
      if (farray) fld = fld[0]; // only handle first-named for this type 
      fld.value = value; 
      break; 
     case "select-one": 
     case "select-multiple": 
      if (farray) fld = fld[0]; // only handle first-named for this type 
      for (var o = 0; o < fld.options.length; ++o) { 
       var opt = fld.options[o]; 
       var oval = opt.value; 
       if (oval == null || oval == "") oval = opt.text; 
       if (oval == value) { 
        opt.selected = true; 
        break; 
       } 
      } 
      break; 
     case "checkbox": 
     case "radio": 
      if (!farray) { 
       // single checbox or radio of that name: 
       fld.checked = true; 
      } else { 
       for (var cr = 0; cr < fld.length; ++cr) { 
        if (fld[cr].value == value) { 
         fld[cr].checked = true; 
         break; 
        } 
       } 
      } 
      break; 
     default: 
      alert("Unknown field type encountered for field " + name + ": " + ftype); 
      break; 
     } // end of switch 
    } // end of loop on fields from qs 
} 
+0

根据查询字符串显示预先填充的代码 – Ian 2013-04-26 17:56:48

+0

从有些模糊的信息看来,如果您只是将信息直接从QS输入到来源,而无需对其进行任何预处理(否则,您已将其转换为已知的下拉列表值)。 – Psytronic 2013-04-26 19:07:24

+0

对不起,我原来的帖子含糊不清。第一次在这里!所以,@ psytronic,你说我应该添加东西来预处理来自查询的内容? – pedroargenti 2013-04-27 08:47:45

回答

2

当你预先填充(我猜你比较在for循环值),与.toLowerCase()比较值。例如:

if (querystringValue.toLowerCase() === optionValue.toLowerCase()) { 
    // Select this option 
    break; 
} 

UPDATE:

为您更新的代码,我认为这将是在这里:

if (oval == value) { 
    opt.selected = true; 
    break; 
} 

所以将其更改为:

if (oval.toLowerCase() === value.toLowerCase()) { 
    opt.selected = true; 
    break; 
} 

根据如果这适用,你可能想对你的复选框/收音机做同样的事情按钮设置:

if (fld[cr].value == value) { 

但这真的取决于你。

+0

所以最好改变我预先填充的方式,而不是添加多个值。有趣!我添加了我的代码预填充。你能帮我解决这个问题吗? – pedroargenti 2013-04-27 12:45:10

+0

@pedroargenti你需要改变的部分是'if(oval == value){'to'if(oval.toLowerCase()== value.toLowerCase()){' – 2013-04-27 18:11:01

+1

@ GabyakaG.Petrioli哈哈谢谢,我从字面上得到你的评论,因为我正在编辑我的答案来解释(在我重新格式化OP的代码,所以我可以读/找到它):) – Ian 2013-04-27 18:11:42