javascript - 我需要为 javascript 数组设置默认选项(下拉字段)

标签 javascript arrays

我继承了这个代码,该代码在我的在线结帐中填充了国家/地区。显示的第一个值是澳大利亚,但我需要将其设置为美国。

<script type=“text/javascript”>
    $(function(){
        $('#FormField_11>option, #FormField_21>option').each(function () {
            // PUT THE COUNTRIES YOU WANT TO KEEP IN THE LIST HERE
            var countries = [ 'United States', 'Canada', 'Belgium', 'Australia', 'Denmark', 'Finland', 'Germany', 'Ireland', 'Italy', 'Japan', 'Netherlands', 'Netherlands Antilles', 'New Zealand', 'Norway', 'Sweden', 'United Kingdom', 'Austria'  ];

            var country = $(this).text();
            if (countries.indexOf(country) == -1) {
                $(this).remove();
            }
        });
    });
</script>​

谁能帮我将美国定义为默认值?

提前致谢

最佳答案

删除所有不需要的国家后,将下拉列表的值设置为所需的默认值:

$(function(){
    $('#FormField_11>option, #FormField_21>option').each(function () {
        // PUT THE COUNTRIES YOU WANT TO KEEP IN THE LIST HERE
        var countries = [ 'United States', 'Canada', 'Belgium', 'Australia', 'Denmark', 'Finland', 'Germany', 'Ireland', 'Italy', 'Japan', 'Netherlands', 'Netherlands Antilles', 'New Zealand', 'Norway', 'Sweden', 'United Kingdom', 'Austria'  ];

        var country = $(this).text();
        if (countries.indexOf(country) == -1) {
            $(this).remove();
        }
    });
    $("#FormField_11, #FormField_21").value("United States");
});

关于javascript - 我需要为 javascript 数组设置默认选项(下拉字段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58400700/

相关文章:

php - 数组 ||非法偏移类型

javascript - 我可以/应该根据元素的数量为下拉菜单栏设置动态高度吗?

javascript - 如何序列化 Ruby 的 TZInfo 数据?

javascript - 无法为 <md-app-content> vue 设置样式

javascript - 引用事件监听器中的工具提示以使其在悬停时保持打开状态

javascript - 如何在 highcharts 饼图中动态居中图表标题位置

javascript - 如何通过高地从远程 JSON 数组创建对象流?

javascript - 在 javascript 中,stringify 数组不返回预期的行为

python - 连接大的 numpy 数组

java - 如何使用自定义对象创建 libgdx 数组的副本?