如何在Javascript中用点替换逗号

如何在Javascript中用点替换逗号,javascript,jquery,Javascript,Jquery,我有一个带有多个选项的html select元素,当选中时,javascript将显示特定的输入字段。在这些字段中,我使用javascript函数将逗号(,)更改为点(.) 问题是,只有id为#size的输入字段才能工作,当选择其他字段时,不会发生任何更改。我正在使用jQuery 选择。。。 大小 重量 尺寸 Javascript: $(document).ready(function() { $("#switch").change(function() { //i

我有一个带有多个选项的html select元素,当选中时,javascript将显示特定的输入字段。在这些字段中,我使用javascript函数将逗号(,)更改为点(.)

问题是,只有id为
#size
的输入字段才能工作,当选择其他字段时,不会发生任何更改。我正在使用jQuery


选择。。。
大小
重量
尺寸
Javascript:

$(document).ready(function() {
    $("#switch").change(function() {
        //if selected option is size
        if ($(this).val() == "size") {
            $(".switch-body").html('<input type="text" id="size" placeholder="Product Size">');
        }
        //if selected option is weight
        else if ($(this).val() == "weight") {
            $(".switch-body").html('<input type="text" id="weight" placeholder="Product Weight">');
        }
        //if selected option is weight
        else if ($(this).val() == "dimensions") {
            $(".switch-body").html(`
            <input type="text" id="height" placeholder="Product Height">
            <input type="text" id="width" placeholder="Product Width">
            <input type="text" id="length" placeholder="Product Length">
            `);
        }
        //if selected option is none
        else if ($(this).val() == "") {
            $(".switch-body").html("");
        }
    });

    //replaces comma with dot (here is the problem)
    $(".switch-body").keyup(function(){
        $("#size").val($("#size").val().replace(/,/g, '.'));
        $("#weight").val($("#weight").val().replace(/,/g, '.'));
        $("#height").val($("#height").val().replace(/,/g, '.'));
        $("#width").val($("#width").val().replace(/,/g, '.'));
        $("#length").val($("#length").val().replace(/,/g, '.'));
    });
});
$(文档).ready(函数(){
$(“#开关”).change(函数(){
//如果选择的选项是“大小”
if($(this.val()=“size”){
$(“.switch body”).html(“”);
}
//如果选择的选项是“重量”
else if($(this.val()=“weight”){
$(“.switch body”).html(“”);
}
//如果选择的选项是“重量”
else if($(this).val()=“维度”){
$(“.switch body”).html(`
`);
}
//如果所选选项为“无”
else if($(this).val()=“”){
$(“.switch body”).html(“”);
}
});
//将逗号替换为点(问题在于此)
$(“.switch body”).keyup(函数(){
$(“#size”).val($(“#size”).val().replace(/,/g,');
$(“#weight”).val($(“#weight”).val()。替换(/,/g');
$(“#height”).val($(“#height”).val().replace(/,/g');
$(“#宽度”).val($(“#宽度”).val().replace(/,/g,,);
$(“#length”).val($(“#length”).val().replace(/,/g');
});
});

此javascript代码之外的其他输入字段上的replace函数工作正常。

问题在于您的
replace()
逻辑试图处理
未定义的
值,而这些字段在DOM中尚不存在。它似乎只适用于
#size
,因为它是列表中的第一个。如果您在控制台“工作”后检查控制台,您会发现
#weight
中的更换导致错误

要解决此问题,请在所有
输入
元素上放置一个公共类,然后为
val()
提供一个函数,该函数返回新值,以根据当前值更新字段,如下所示:

$(".switch-body").keyup(function() {
  $(".no-comma").val(function(i, v) {
    return v.replace(/,/g, '.');
  });
});
$(文档).ready(函数(){
$(“#开关”).change(函数(){
//如果选择的选项是“大小”
if($(this.val()=“size”){
$(“.switch body”).html(“”);
}
//如果选择的选项是“重量”
else if($(this.val()=“weight”){
$(“.switch body”).html(“”);
}
//如果选择的选项是“重量”
else if($(this).val()=“维度”){
$(“.switch body”).html(`
`);
}
//如果所选选项为“无”
else if($(this).val()=“”){
$(“.switch body”).html(“”);
}
});
//将逗号替换为点(问题在于此)
$(“.switch body”).keyup(函数(){
$(“.no逗号”).val(函数(i,v){
返回v.替换(/,/g');
});
});
});

类型

选择。。。 大小 重量 尺寸
首先要做的是检查控制台。代码失败是因为您试图替换的某些元素未定义,因为每次更改选择时都会创建和销毁HTML元素。更好的方法是隐藏和显示元素

示例代码:


类型

选择。。。 大小 重量 尺寸

代码就在这里。

感谢您在代码中解释我的问题。现在一切都很好@Rory McCrossan!
$(".switch-body").keyup(function() {
  $(".no-comma").val(function(i, v) {
    return v.replace(/,/g, '.');
  });
});
$(document).ready(function() {
    $(".switch-body").children().each(function() {
      $(this).hide();
    });
    $("#switch").change(function() {
            $(".switch-body").children().each(function() {
            $(this).hide();
        });
        //if selected option is size
        if ($(this).val() == "size") {
            $(".show-size").show();
        }
        //if selected option is weight
        else if ($(this).val() == "weight") {
            $(".show-weight").show();
        }
        //if selected option is weight
        else if ($(this).val() == "dimensions") {
            $(".show-dimensions").show();
        }
    });

    //replaces comma with dot (here is the problem)
    $(".switch-body").keyup(function(){
        $("#size").val($("#size").val().replace(/,/g, '.'));
        $("#weight").val($("#weight").val().replace(/,/g, '.'));
        $("#height").val($("#height").val().replace(/,/g, '.'));
        $("#width").val($("#width").val().replace(/,/g, '.'));
        $("#length").val($("#length").val().replace(/,/g, '.'));
    });
});