我的表单使用来自查询字符串的值预填充 onload。在查询中,当 country=Australia 时,选择“Australia”。但是当 country=AUSTRALIA 或 country=australia 时,不会选择任何内容。
我的选择字段:
<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">
会让它工作。我该怎么做?这(可以选择标签中的选项携带多个值吗?)是一个选项吗?
否则,如何使选择字段对查询字符串值的预填充不区分大小写?
用于查询字符串的 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
}