简体   繁体   English

将多个选项添加到多个选择js时出现问题

[英]Problem while adding many options to multiple select js

I'm pretty new to JS, and I want to add many options to multiple select. 我是JS的新手,我想为多个选择添加许多选项。 But the problem is that when I'm trying to show them only the last record is triggering the function (showing). 但是问题是,当我尝试显示它们时,只有最后一条记录才触发该函数(显示)。 But when I'm doing console.log it is showing every thing perfectly. 但是,当我执行console.log时,它完美地显示了每件事。

This is my HTML: 这是我的HTML:

<form class="cart" method="post" action="{{ route('cart.add') }}">
{{ csrf_field() }}
<div class="form-group">
    <label for="product_size" class="grey">Model</label>
    <span class="red">*</span>
    <select class="form-control" id="productModel" name="product" onchange=" addRelatedproducts();">
        <option value="">Wybierz model produktu...</option>
        @foreach($productModels as $productModel)
            <option value="{{$productModel->id}}">{{$productModel->modelName}} - {{$productModel->modelPrice}} @if($productModel->modelPriceCurrency === 1) PLN @else EUR @endif</option>
        @endforeach
    </select>
</div>
<div class="form-group">
    <label for="exampleSelect1">Ilość:</label>
    <select class="form-control" id="exampleSelect1" name="quantity">
        <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>
</div>
<div>
    <div class="form-group" id="relatedProductsDiv" style="display: none;">
        <label for="exampleSelect1">Produkty powiązane:</label>
        <select id="select" multiple="multiple" class="relatedProducts" name="relatedProduct">

        </select>
    </div>
</div>

This is my JS: 这是我的JS:

function addRelatedproducts(){
var model = [
<?php foreach($productModels as $productModel):?>
  <?=$productModel?>,
<?endforeach;?>
];

var relatedProducts = [
    <?php foreach($relatedProductArray as $relatedProduct): ?>
        <?=$relatedProduct ?>,
    <?endforeach; ?>
];

var e = document.getElementById("productModel");
var selectedModelId = e.options[e.selectedIndex].value;
var select = document.getElementById("select");


for (var i = 0; i < relatedProducts.length + 1; i++) {

    select.remove(relatedProducts[i].relatedProductName);

    if(parseInt(relatedProducts[i].model_id) === parseInt(selectedModelId)){
        console.log(relatedProducts[i])
        console.log(selectedModelId)

        document.getElementById("relatedProductsDiv").style.display = "";

        var option = document.createElement("option");
        option.value = relatedProducts[i].id;
        option.text = relatedProducts[i].relatedProductName;
        select.add(option);

    }
    else{
        document.getElementById("relatedProductsDiv").style.display = "none";
    }
}

} }

I don't really know why it isn't working. 我真的不知道为什么它不起作用。 Can anybody help me with this problem? 有人可以帮我解决这个问题吗?

select.remove(relatedProducts[i].relatedProductName);

select.remove expects an option index as an argument. select.remove需要选项索引作为参数。 And I suppose when it receives a product name string (probably not a number) apparently it evaluates it as NaN and just removes the very first option in the select on every step. 而且我想当它收到产品名称字符串(可能不是数字)时,显然会将其评估为NaN并在每一步中仅删除选择中的第一个选项。 So it comes up with an empty select in the end. 因此,最后它带有一个空选择。

Maybe the better approach would be to clear all the select options right before iterating relatedProducts and then to populate only with the needed ones? 也许更好的方法是在迭代relatedProducts之前清除所有选择选项,然后仅填充所需的选项?

while(select.options.length) {
    select.remove(0);
}
for (var i = 0; i < relatedProducts.length + 1; i++) {

    if(parseInt(relatedProducts[i].model_id) === parseInt(selectedModelId)){
        console.log(relatedProducts[i])
// ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM