3

我的页面上有两个选择字段 selectA 和 selectB。现在我想通过单击一个链接来预先选择它们:http://www.mypage.com/index.html?selectA=a132&selectB=b02#bestellung我使用 javascript 管理解码并将 a132 和 b02 写入 var idA 和 var idB。但是如何使用这些 var 来触发字段的预选呢?a132 是 selectA 选项的 id。b02 是 selectB 选项的 id。

我的代码:

<!-- decode url write vars -->
<script>
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&#]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
</script>	

<!-- set preselect -->
  <script>
var idA = getUrlVars()["selectA"];
var idB = getUrlVars()["selectB"];

document.getElementById("selectA").selectedId = idA;
document.getElementById("selectB").selectedId = idB;
</script>



<!-- update Image for documentation only -->
  <script>
function updateImage(id, srcCallback){
	var img = document.getElementById(id);
	if (img){
		img.src = srcCallback();
	}
}

function getImageSrc(form){
	var sel1 = form.Size;
	var sel2 = form.Options;
	return "img/preview/" + sel1.options[sel1.selectedIndex].id + sel2.options[sel2.selectedIndex].id + ".jpg";
}
</script>
<select id="selectA" name="Size" onchange="ReadForm (this.form, false);updateImage('image1', getImageSrc.bind(undefined, this.form));BeschreibungA();" required class="form-control">
  <option value="+0.00" id="a00" selected>1. Please select:</option>
    <option value="wei&szlig; +15.00 Eur" id="a130">wei&szlig; 15,00 EUR</option>
	<option value="wei&szlig; | gold +10.00 Eur" id="a131">wei&szlig; | gold 10,00 EUR</option>
	<option value="gr&uuml;n | gold +12.00 Eur" id="a132">gr&uuml;n | gold 12,00 EUR</option>
	<option value="antrazith +15.00 Eur" id="a133">antrazith 15,00 EUR</option>
	<option value="natur | gold +13.00 Eur" id="a134">natur | gold 13,00 EUR</option>
	<option value="UJack +18.00 Eur" id="a135">Union Jack 18,00 EUR</option>
	<option value="StG +18.00 Eur" id="a136">StG 18,00 EUR</option>
	<option value="Streifen +18.00 Eur" id="a137">Streifen 18,00 EUR</option>
	<option value="Gold +18.00 Eur" id="a138">Gold 18,00 EUR</option>
  </select>


<br>
<select id="selectB" name="Options" data-parent-id="selectA" onchange="ReadForm (this.form, false);updateImage('image1', getImageSrc.bind(undefined, this.form));BeschreibungB();" required class="form-control">>
  <option value="+0.00" id="b99" selected >2. Please select:</option>
  <option value="wei&szlig; +3.00 Eur inkl. Versand und 19% MWSt." id="b01" data-parent-option-ids="a00,a130,a131,a135">wei&szlig; (E14) 3,00 Eur</option>
  <option value="orange +3.00 Eur inkl. Versand und 19% MWSt." id="b02" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a138">orange (E14) 3,00 Eur</option>
  <option value="pink +3.00 Eur inkl. Versand und 19% MWSt." id="b03" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a138">pink (E14) 3,00 Eur</option>
  <option value="rot +3.00 Eur inkl. Versand und 19% MWSt." id="b04" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a135,a136,a137,a138">rot (E14) 3,00 Eur</option>
  <option value="PDot +3.00 Eur inkl. Versand und 19% MWSt." id="b05" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a138">polka dot rot (E14) 3,00 EUR</option>
  <option value="VK Rot +3.00 Eur inkl. Versand und 19% MWSt." id="b06" data-parent-option-ids="a00,a130,a131,a132,a133">Vichy Karo rot (E14) 3,00 EUR</option>
  <option value="VK Blau +3.00 Eur inkl. Versand und 19% MWSt." id="b07" data-parent-option-ids="a00,a130,a131,a132,a133">Vichy Karo blau (E14) 3,00 EUR</option>
  <option value="taubengraublau +3.00 Eur inkl. Versand und 19% MWSt." id="b08" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a138">taubengraublau (E14) 3,00 EUR</option>
  <option value="nachtblau +3.00 Eur inkl. Versand und 19% MWSt." id="b09" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a135,a136,a138">nachtblau (E14) 3,00 EUR</option>
  <option value="dunkelgr&uuml;n +3.00 Eur inkl. Versand und 19% MWSt." id="b10" data-parent-option-ids="a00,a130,a131,a133,a134,a138">dunkelgr&uuml;n (E14) 3,00 EUR</option>
  <option value="golden +4.00 Eur inkl. Versand und 19% MWSt." id="b11" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a136,a137,a138">gold (E14) 4,00 EUR</option>
  </select>

感谢您的帮助,乔治

4

1 回答 1

1

如果您选择的所有选项都有 id,那么您可以简单地执行以下操作:

document.querySelector('#' + idA).selected = true
document.querySelector('#' + idB).selected = true
于 2016-08-23T11:50:46.060 回答