通过javascript选中填充下拉值选择的复选框

我在 6 个场景中得到了我想要做的 6 个复选框:


    <tr style="height: 21px;">

<td style="width: 25%; height: 21px;">COB</td>

<td style="width: 25%; height: 21px;"><input name="COB" type="checkbox" id="COB" value="1" <?php if($_GET['COB'] == '1'){  echo 'checked="checked"';}?>/>           </td>

<td style="width: 25%; height: 21px;">SMT</td>

<td style="width: 25%; height: 21px;">  <input name="SMT" id="SMT" type="checkbox" value="1" <?php if($_GET['SMT'] == '1'){  echo 'checked="checked"';}?>/> </td>

</tr>

<tr style="height: 21px;">

<td style="width: 25%; height: 21px;"></td>

<td style="width: 25%; height: 21px;"></td>

<td style="width: 25%; height: 21px;">BGA</td>

<td style="width: 25%; height: 21px;">  <input name="BGA" id="BGA" type="checkbox" value="1" <?php if($_GET['BGA'] == '1'){  echo 'checked="checked"';}?>/> </td>

</tr>

<tr style="height: 21px;">

<td style="width: 25%; height: 21px;"></td>

<td style="width: 25%; height: 21px;"></td>

<td style="width: 25%; height: 21px;">  TSOP Typ 1 </td>

<td style="width: 25%; height: 21px;"><input name="TSOP" id="TSOP" type="checkbox" value="1"<?php if($_GET['TSOP'] == '1'){  echo 'checked="checked"';}?> /></td>

</tr>

<tr style="height: 21px;">

<td style="width: 25%; height: 21px;"></td>

<td style="width: 25%; height: 21px;"></td>

<td style="width: 25%; height: 21px;"> TSOP Typ 2</td>

<td style="width: 25%; height: 21px;"><input name="TSOP2" id="TSOP2" type="checkbox" value="2"<?php if($_GET['TSOP'] == '2'){  echo 'checked="checked"';}?> />   </td>


</tr>

<tr style="height: 21px;">

<td style="width: 25%; height: 21px;"></td>

<td style="width: 25%; height: 21px;"></td>

<td style="width: 25%; height: 21px;"> LGA</td>

<td style="width: 25%; height: 21px;"><input name="LGA" id="LGA" type="checkbox" value="1"<?php if($_GET['LGA'] == '1'){  echo 'checked="checked"';}?> />    </td>

</tr>


UYOU
浏览 199回答 3
3回答

忽然笑

我注意到在最后 3 个复选框中,值和 php 代码之间没有空格。这:<td&nbsp;style="width:&nbsp;25%;&nbsp;height:&nbsp;21px;"><input&nbsp;name="TSOP2"&nbsp;id="TSOP2"&nbsp;type="checkbox"&nbsp;value="2"<?php&nbsp;if($_GET['TSOP']&nbsp;==&nbsp;'2'){&nbsp;&nbsp;echo&nbsp;'checked="checked"';}?>&nbsp;/>&nbsp;&nbsp;&nbsp;</td>php执行后可以去这个:<td&nbsp;style="width:&nbsp;25%;&nbsp;height:&nbsp;21px;"><input&nbsp;name="TSOP2"&nbsp;id="TSOP2"&nbsp;type="checkbox"&nbsp;value="2"checked="checked"&nbsp;/>&nbsp;&nbsp;&nbsp;</td>应该是这样的:td&nbsp;style="width:&nbsp;25%;&nbsp;height:&nbsp;21px;"><input&nbsp;name="TSOP2"&nbsp;id="TSOP2"&nbsp;type="checkbox"&nbsp;value="2"&nbsp;checked="checked"&nbsp;/>&nbsp;&nbsp;&nbsp;</td>

ibeautiful

根据DOM 标准,elemnet ID 在整个文档中应该是唯一的。在您的代码中,两个input具有相同的 id TSOP。我已经为您创建了 codepen示例以显示 JAVASCRIPT 部分正在工作,您的 PHP 代码可能存在一些问题。function FlashFunction() {&nbsp; var index = document.getElementById("FlashID").selectedIndex;&nbsp; var SMT = document&nbsp; &nbsp; .getElementById("FlashID")&nbsp; &nbsp; .options[index].getAttribute("data-SMT");&nbsp; var BGA = document&nbsp; &nbsp; .getElementById("FlashID")&nbsp; &nbsp; .options[index].getAttribute("data-BGA");&nbsp; var TSOP = document&nbsp; &nbsp; .getElementById("FlashID")&nbsp; &nbsp; .options[index].getAttribute("data-TSOP");&nbsp; var LGA = document&nbsp; &nbsp; .getElementById("FlashID")&nbsp; &nbsp; .options[index].getAttribute("data-LGA");&nbsp; document.getElementsByName("COB")[0].value = COB;&nbsp; document.getElementsByName("SMT")[0].value = SMT;&nbsp; document.getElementsByName("BGA")[0].value = BGA;&nbsp; document.getElementsByName("TSOP")[0].value = TSOP;&nbsp; document.getElementsByName("TSOP2")[0].value = TSOP;&nbsp; document.getElementsByName("LGA")[0].value = LGA;&nbsp;&nbsp;&nbsp; if (TSOP == 1) {&nbsp; &nbsp; document.getElementsByName("TSOP")[0].checked = true;&nbsp; }}<select id="FlashID" name="FlashID" onchange="FlashFunction()">&nbsp; <option data-SMT="1" value="volvo">102292</option>&nbsp; <option data-SMT="1" value="saab">102293</option>&nbsp; <option data-SMT="1" data-TSOP="1" value="mercedes">102412</option></select><table>&nbsp; <tr style="height: 21px;">&nbsp; &nbsp; <td style="width: 25%; height: 21px;">COB</td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"><input name="COB" type="checkbox" id="COB" value="1" /> </td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;">SMT</td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"> <input name="SMT" id="SMT" type="checkbox" value="1" </td>&nbsp; </tr>&nbsp; <tr style="height: 21px;">&nbsp; &nbsp; <td style="width: 25%; height: 21px;"></td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"></td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;">BGA</td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"> <input name="BGA" id="BGA" type="checkbox" value="1" </td>&nbsp; </tr>&nbsp; <tr style="height: 21px;">&nbsp; &nbsp; <td style="width: 25%; height: 21px;"></td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"></td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"> TSOP Typ 1 </td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"><input name="TSOP" id="TSOP" type="checkbox" value="1" </td>&nbsp; </tr>&nbsp; <tr style="height: 21px;">&nbsp; &nbsp; <td style="width: 25%; height: 21px;"></td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"></td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"> TSOP Typ 2</td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"><input name="TSOP2" id="TSOP2" type="checkbox" value="2" </td>&nbsp; </tr>&nbsp; <tr style="height: 21px;">&nbsp; &nbsp; <td style="width: 25%; height: 21px;"></td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"></td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;"> LGA</td>&nbsp; &nbsp; <td style="width: 25%; height: 21px;">&nbsp; &nbsp; &nbsp; <input name="LGA" id="LGA" type="checkbox" value="1" </td>&nbsp; </tr></table>

摇曳的蔷薇

你不检查这样的复选框document.getElementsByName("COB")[0].value = COB;document.getElementsByName("SMT")[0].value = SMT;document.getElementsByName("BGA")[0].value = BGA;document.getElementsByName("TSOP")[0].value = TSOP;document.getElementsByName("TSOP2")[0].value = TSOP;document.getElementsByName("LGA")[0].value = LGA;正确的方法是这样的:document.getElementById("ItemId").checked = true;&nbsp;// because there is one <input ...> i use getEmelentById instead of getElementsById&nbsp;在您的程序中,我认为这是您需要使用的正确 javascript 是:<script>function FlashFunction(){var index = document.getElementById("FlashID").selectedIndex;var COB = document.getElementById("FlashID").options[index].getAttribute("data-COB");var SMT = document.getElementById("FlashID").options[index].getAttribute("data-SMT");var BGA = document.getElementById("FlashID").options[index].getAttribute("data-BGA");var TSOP = document.getElementById("FlashID").options[index].getAttribute("data-TSOP");var LGA = document.getElementById("FlashID").options[index].getAttribute("data-LGA");if (COB == 1 ) {&nbsp; &nbsp; document.getElementById("COB").checked= true;} else {&nbsp; &nbsp; document.getElementById("COB").checked= false;}if (SMT == 1 ) {&nbsp; &nbsp; document.getElementById("SMT").checked= true;} else {&nbsp; &nbsp; document.getElementById("SMT").checked= false;}if (BGA == 1 ) {&nbsp; &nbsp; document.getElementById("BGA").checked= true;} else {&nbsp; &nbsp; document.getElementById("BGA").checked= false;}if (TSOP == 1 ) {&nbsp; &nbsp; document.getElementById("TSOP").checked= true;} else {&nbsp; &nbsp; document.getElementById("TSOP").checked= false;}if (TSOP == 2 ) {&nbsp; &nbsp; document.getElementById("TSOP2").checked= true;} else {&nbsp; &nbsp; document.getElementById("TSOP2").checked= false;}if (LGA == 1 ) {&nbsp; &nbsp; document.getElementById("LGA").checked= true;} else {&nbsp; &nbsp; document.getElementById("LGA").checked= false;}}</script>
打开App,查看更多内容
随时随地看视频慕课网APP