有在特定变化问题的其他选择选项的文本未更新

问题描述 投票:3回答:1

要改变的第二选择选项的所有文字与我期望的文本

理由是:

我不想显示价格我的选择,所以我用切片和索引的方法来改变选择文本和替换后的文字“ - ”字符内..但问题是,当我改变第一选择选择其他选择选项显示以前的文本不其中从其他来源来(这不是源在我的手,我只是有HTML页面)最新文本

有两个选择框第一个ID为“orderform类”和其他地方的价格显示“#orderform服务”

这里是我的代码

$(document).ready(function() { 
    $('#orderform-category').change(function() {

        $('#orderform-service option').each(function() {
            var getOption = $(this).text();
            var getPriceIndex = getOption.indexOf("—");
            console.log(getPriceIndex);
            if(getPriceIndex!==-1){
                getOption = getOption.slice(0,getPriceIndex)+"";
            }
            console.log(getOption);
        });

    });
});
javascript jquery twig
1个回答
1
投票

检查这个代码,您可能需要触发器执行的功能,它将执行,当你选择框中的值会发生变化。

  $(function(){
        $('#orderform-service').change(function(){
          var data= $(this).val();
          $('#orderform-service option').each(function() {
                    var getOption = $(this).text();
                    var getPriceIndex = getOption.indexOf("—");
                    if(getPriceIndex!==-1){
                        getOption = getOption.slice(0,getPriceIndex)+"";
                    }
                    console.log(getOption);
                    $(this).text(getOption);
                });           
        });
        $('#orderform-service').trigger('change');
    });
© www.soinside.com 2019 - 2024. All rights reserved.