我的 HTML 为:
====================================
<input type="checkbox" id="Red" onclick="changeURL('WineType', 'Red')"> Red (30499)
<input type="checkbox" id="White" onclick="changeURL('WineType', 'White')"> White (22840)
====================================
首先我调用 changeURL() 然后调用 isChecked() 函数。
但是当这两个函数都被执行时,该复选框仍然未被选中。
====================================
function changeURL(fieldname, facetname) {
var ref= window.location.href ;
if(fieldname == "WineType") {
var matchPrice = location.href.match(/&fq=%7B%21tag%3Ddt1%7DWineType%3A/);
if(matchPrice == '&fq=%7B%21tag%3Ddt1%7DWineType%3A' )
{
var ndStart = ref.indexOf("WineType%3A%28");
var ndEnd = ref.indexOf("%29",ndStart );
ref = ref.substring(0,ndStart+12) + ref.substring(ndStart+12, ndEnd) +" OR " +facetname + ref.substring(ndEnd);
}
else {
ref = ref + "?&fq=%7B%21tag%3Ddt1%7DWineType%3A%28"+facetname+"%29";
}
}
window.location.href = ref;
isChecked(facetname);
};
function isChecked(element)
{
var field = document.getElementById(element);
if (element) {
if (field.checked) {
document.getElementById(element).checked= false;
} else {
document.getElementById(element).checked= true;
}
}
};