Javascript 使选择选项值不区分大小写/具有多个值
我的表单在加载时预先填充querystring中的值。在查询中,当country=Australia时,选择“Australia”。但当country=AUSTRALIA或country=AUSTRALIA时,不会选择任何内容 我的选择字段:Javascript 使选择选项值不区分大小写/具有多个值,javascript,html,Javascript,Html,我的表单在加载时预先填充querystring中的值。在查询中,当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; fon
<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>
请选择国家/地区
澳大利亚
奥地利
我想
会让它工作的。我该怎么做?这个()是一个选项吗
否则,如何使select字段对querystring值的预填充不区分大小写
添加了querystring的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
}
函数填充(表单){
if(location.search==null | | location.search.length<1)返回;//无查询字符串
var pairs=location.search.substring(1).split(“+”);
对于(var p=0;p=1&&fld[0]。类型!=null&&fld[0]。类型!=未定义){
ftype=fld[0]。类型;
法拉利=真;
}否则{
ftype=fld.type;
}
}
开关(ftype){
案例“文本”:
案例“隐藏”:
案例“textarea”:
if(farray)fld=fld[0];//仅此类型第一个命名的句柄
fld.value=值;
打破
案例“选择一个”:
案例“选择多个”:
if(farray)fld=fld[0];//仅此类型第一个命名的句柄
对于(变量o=0;o
当您进行预填充时(我猜您会比较循环中的值),将值与.toLowerCase()
进行比较。例如:
if (querystringValue.toLowerCase() === optionValue.toLowerCase()) {
// Select this option
break;
}
更新:
对于您的更新代码,我想应该是:
if (oval == value) {
opt.selected = true;
break;
}
因此,将其更改为:
if (oval.toLowerCase() === value.toLowerCase()) {
opt.selected = true;
break;
}
根据是否适用,您可能希望对复选框/单选按钮设置执行相同的操作:
if (fld[cr].value == value) {
但是这真的取决于你。显示你的代码,你在哪里根据查询字符串进行预填充。从有些模糊的信息来看,如果你只是直接从QS将信息输出到源代码中,而没有对其进行任何预处理,那么你似乎已经在那里找到了一个XSS入口点(否则,您已经将其转换为下拉列表的已知值)。抱歉我的原始帖子含糊不清。这里是第一次!那么,@psytronic,你是说我应该添加一些东西来预处理来自查询的内容?所以最好改变预填充的方式,而不是添加多个值。有趣!我添加了预填充的代码。你能帮我定位这个条件吗?@pedrorgenti the part yo你需要改变的是if(oval==value){
到if(oval.toLowerCase()==value.toLowerCase()){
@gabykag.Petrioli哈哈,谢谢,我在编辑我的答案时收到了你的评论(在我重新格式化OP的代码以便阅读/找到它之后):)