如何用JS选择一个选择框选项值?

时间:2019-05-28 06:24:39

标签: javascript

这听起来似乎很简单,但是我一直很想让它开始工作。有人请帮忙。我的选择框如下所示。如何从列表中选择值3?

        if(!empty($listeDoc))
        {
            $iGroups = 0;
            while($iGroups < count($listeDoc))
            {
            ?>  
            <tr>
                <td>
                    <input type="checkbox" name="productSelect[<?php echo $listeDoc[$iGroups]->DOC_CLIENT_ID ?>]['product_id']" id="productSelectCreation_<?php echo $listeDoc[$iGroups]->DOC_CLIENT_ID ?>" onchange="setValueToOne(this)"  ><?php echo $listeDoc[$iGroups]->DOC_CLIENT_NOM; ?>
                </td>

                <td>
                    <select name="productSelect[<?php echo $listeDoc[$iGroups]->DOC_CLIENT_ID ?>]['quantity']" id="productQty_<?php echo $listeDoc[$iGroups]->DOC_CLIENT_ID ?>" class="form-control1" value="<?php echo $qty; ?>" style="width:100%; height:25px; border : #62270c solid 1px;" tabindex="1">
                        <option value=""/></option>
                        <option value="1"<?php if ($qty == 1) echo 'selected="selected"'; ?>>1</option>
                        <option value="2"<?php if ($qty == 2) echo 'selected="selected"'; ?>>2</option>
                        <option value="3"<?php if ($qty == 3) echo 'selected="selected"'; ?>>3</option>
                        <option value="4"<?php if ($qty == 4) echo 'selected="selected"'; ?>>4</option>
                        <option value="5"<?php if ($qty == 5) echo 'selected="selected"'; ?>>5</option>
                    </select>
                <td>
            </tr>

            <?php
            $iGroups++;
            }
        }

这是我尝试过的:

函数setValueToOne(值) {     复选框= document.getElementById('productSelectCreation');

if (checkbox.checked == true)
{
    document.getElementById("productQtyCreation").value = 1;
}
else
    document.getElementById("productQtyCreation").value = '0';

}

3 个答案:

答案 0 :(得分:1)

看来您正在寻找类似的东西。

function setValueToOne(chkbox)
{ 
   if (chkbox.checked) {
      alert("checked");
      alert(chkbox.value);
      document.getElementById("productQtyCreation").value = chkbox.value;
    } else {
      
      alert("Unchecked");
      document.getElementById("productQtyCreation").value = 0
    }
}
<select name="productSelect" id="productQtyCreation" class="form-control1">
    <option value=""/></option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

<input type="checkbox" name="productSelect" id="productSelectCreation" onClick="setValueToOne(this)" value="3" />Test

答案 1 :(得分:0)

如果我理解正确,则想使用香草javascript更改选择框的选定选项。您将需要循环选择框并比较值。

 document.getElementById('productSelectCreation').onclick = function() {
    var val;
    var checkbox = document.getElementById('productSelectCreation');
    var sel = document.getElementById('productQtyCreation');
if (checkbox.checked)
{
    val = "3";
}
else
    val = "0";
}

      var opts = sel.options;
      for (var opt, j = 0; opt = opts[j]; j++) {
        if (opt.value == val) {
          sel.selectedIndex = j;
          break;
        }
      }
    }

答案 2 :(得分:-2)

function setValueToOne(checkbox) {
    if(checkbox.checked) {
        document.getElementById('productQtyCreation').value = 3
    }else{
        document.getElementById('productQtyCreation').value = 0
    }
}
<select id="productQtyCreation">
    <option disabled selected value=""></option>
    <option value="0">Zero</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>
<input type="checkbox" name="productSelect" id="productSelectCreation" onClick="setValueToOne(this)" value="3" />