Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 4-如何使用Javascript删除细枝上的选项?_Javascript_Php_Symfony_Select_Choice - Fatal编程技术网

Symfony 4-如何使用Javascript删除细枝上的选项?

Symfony 4-如何使用Javascript删除细枝上的选项?,javascript,php,symfony,select,choice,Javascript,Php,Symfony,Select,Choice,我的表格里有一个symfony4项目。 在我的formType中,我选择了如下类型: ->add('momentDebut', ChoiceType::class, [ 'choices' => [ "matin" => "matin", "après-midi" => "après-midi", "journée" =&

我的表格里有一个symfony4项目。 在我的formType中,我选择了如下类型:

->add('momentDebut', ChoiceType::class, [
                'choices' => [
                    "matin" => "matin",
                    "après-midi" => "après-midi",
                    "journée" => "journée"
                ],
在我的小枝中,我有一个选择:

<select class="browser-default custom-select" id="choiceUser" onchange="choiceDay(this)">
                <option value="" disabled selected>Choisissez la durée de l'absence</option>
                <option value="jour">La journée</option>
                <option value="periode">Du xx au xx</option>
            </select>
这似乎很有效! 问题在于这一行:

targetOption = targetSelect.querySelector('option[value="journée"]');
我把它换成了

var targetOption = document.querySelector('[name="absence[momentDebut]"][value="journée"]');
现在,一切都好了

谢谢你的帮助

虽然并不是像您要求的那样完全删除
选项,但一种可能的解决方案是禁用它,防止被选中,这稍微容易一些

函数选择日期(el){
targetRadio=document.querySelector('[name=“缺勤[时刻首秀]”]:选中“);
targetOption=document.querySelector('[name=“缺席[时刻首秀]”][value=“日记”]);
如果(el.value==“周期”){
if(targetOption==targetRadio){
//如果当前已选中,请取消选中
targetOption.checked=false;
}
//禁用选择
targetOption.setAttribute('disabled',true);
}否则{
targetOption.removeAttribute(“已禁用”);
}
}

缺席期间的选择
杂志
杜xx au xx
马汀
四月中旬
日记

有什么问题吗?针对元素?您没有显示任何javascript,所以我假设它正常工作?是的,我希望第一次以元素值为目标。并删除最后一个选项。我将添加我的Javascript,除非您更改了
表单主题
,否则我认为您的选择器不会匹配任何元素,其中一些元素甚至不在问题中。但是没有,您有语法错误,
removeAttribute
应该在
大小写“periode”
块之外,并删除
else
。如果您将相关的呈现html添加到问题中而不是表单定义中,会更容易。我认为问题在于,在我的源代码中,我没有任何选择/选项(matin,après-midi,Journeée),我将添加我以前的内容是,您没有在类型中包含
'expanded'=>true
,因此我假设它是常规选择。我也更新了我的答案,但基本上都是一样的。谢谢你的回复,我将用我以前的javascript更新我的第一篇文章,还有我实际的小树枝文件,因为我对这段代码有一些疑问
function choiceDay(select) {
            var valeur = select.options[select.selectedIndex].value;
            var targetSelect = document.querySelector('[name="absence[momentDebut]"]');
            var targetOption = document.querySelector('[name="absence[momentDebut]"][value="journée"]');
            console.log(targetSelect.value);
            console.log(targetOption);


            // var choice = $('#choiceMomentDebut').text();
            // console.log(choice);

            switch (valeur) {
                case "periode":
                    document.getElementById("absence").style.visibility = "visible";
                    document.getElementById("momentDebut").style.visibility = "visible";
                    document.getElementById("momentFin").style.visibility = "visible";

                    if (targetOption.value == "journée") { // Reset the selection to the default if it's currently selected
                        targetSelect.selectedIndex = 0;
                    }
                    // Disable the selection
                    targetOption.setAttribute('disabled', true);

                    break;

                case "jour":
                    document.getElementById("absence").style.visibility = "visible";
                    document.getElementById("momentDebut").style.visibility = "visible";
                    document.getElementById("momentFin").style.visibility = "hidden";
                    targetOption.removeAttribute('disabled');
                    break;

                default:
                    break;
            }
        }
targetOption = targetSelect.querySelector('option[value="journée"]');
var targetOption = document.querySelector('[name="absence[momentDebut]"][value="journée"]');