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

时间:2019-05-27 18:47:32

标签: javascript php html checkbox drop-down-menu

在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="TSOP" id="TSOP" 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>

这里是PHP部分,我是如何填充下拉列表的。

<td style="width: 14.2857%; height: 21px;">  <select id="FlashID" name="FlashID" onchange="FlashFunction()" size="1" >

                    <option disabled selected value> </option>;                             
            <?php
            foreach($connection->query($flash) as $m)
            {
                        if($m['FlashID'] == $_GET['FlashID']){
                $isSelected = 'selected="selected"';
            }else{
                $isSelected = '';
            }
            echo "<option data-COB='".$m['COB']."' data-SMT='".$m['SMT']."' data-BGA='".$m['BGA']."' data-TSOP='".$m['TSOP']."' data-LGA='".$m['LGA']."' value='" . $m['FlashID'] . "'".$isSelected."  >" .$m['SAP'] ."</option>";

            }
            ?> 
            </td>

这是填充的下拉列表的SQL表

FlashID   SAP   COB   SMT   BGA   TSOP    LGA
1        102292  0     1     0     2       0
3        102293  0     1     0     2       0
4        102294  0     1     0     2       0
5        102296  0     1     0     0       1
6        102412  0     1     0     1       0
7        102413  0     1     0     1       0
8        102414  0     1     0     1       0
9        102651  0     1     0     2       0
10       102652  0     1     0     2       0
11       102664  0     1     0     2       0

这是我无法使用的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");
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("LGA")[0].value = LGA;
}
</script>

请注意,TSOP的值可以为1或2。

例如。您已经看到我是否选择了FlashID6。应该选中TSOP和SMT复选框。

但是atm我不知道,为什么我的Javascript不起作用,我该如何处理呢?有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

我注意到在最后3个复选框中,值和php代码之间没有空格。

此:

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

在执行php后可以执行以下操作:

<td style="width: 25%; height: 21px;"><input name="TSOP2" id="TSOP2" type="checkbox" value="2"checked="checked" />   </td>

,应该像这样:

td style="width: 25%; height: 21px;"><input name="TSOP2" id="TSOP2" type="checkbox" value="2" checked="checked" />   </td>

答案 1 :(得分:0)

根据DOM Standard,elemnet ID在整个文档中应该是唯一的。在您的代码中,两个input具有相同的ID TSOP

我已经创建了sample的代码段,以供您证明JAVASCRIPT部分正在工作,您的PHP代码可能存在问题。

function FlashFunction() {

  var index = document.getElementById("FlashID").selectedIndex;


  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");


  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;
  
  if (TSOP == 1) {
    document.getElementsByName("TSOP")[0].checked = true;
  }
}
<select id="FlashID" name="FlashID" onchange="FlashFunction()">
  <option data-SMT="1" value="volvo">102292</option>
  <option data-SMT="1" value="saab">102293</option>
  <option data-SMT="1" data-TSOP="1" value="mercedes">102412</option>
</select>
<table>
  <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" /> </td>
    <td style="width: 25%; height: 21px;">SMT</td>
    <td style="width: 25%; height: 21px;"> <input name="SMT" id="SMT" type="checkbox" value="1" </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" </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" </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" </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" </td>
  </tr>
</table>

答案 2 :(得分:0)

您不会选中这样的复选框

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; 
// because there is one <input ...> i use getEmelentById instead of getElementsById 

在您的程序中,我认为这是您需要使用的正确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 ) {
    document.getElementById("COB").checked= true;
} else {
    document.getElementById("COB").checked= false;
}
if (SMT == 1 ) {
    document.getElementById("SMT").checked= true;
} else {
    document.getElementById("SMT").checked= false;
}
if (BGA == 1 ) {
    document.getElementById("BGA").checked= true;
} else {
    document.getElementById("BGA").checked= false;
}
if (TSOP == 1 ) {
    document.getElementById("TSOP").checked= true;
} else {
    document.getElementById("TSOP").checked= false;
}
if (TSOP == 2 ) {
    document.getElementById("TSOP2").checked= true;
} else {
    document.getElementById("TSOP2").checked= false;
}
if (LGA == 1 ) {
    document.getElementById("LGA").checked= true;
} else {
    document.getElementById("LGA").checked= false;
}

}
</script>