【问题标题】:jquery get input value to retrieve matching array values to populate hidden inputsjquery 获取输入值以检索匹配的数组值以填充隐藏输入
【发布时间】:2013-10-16 23:20:22
【问题描述】:

寻找有关用于提取存储在数组中的纬度/经度值的表单的一些指导。代码示例不完整(抱歉),只是处于粗略阶段。

<script type="text/javascript">
$document.ready(function(){
    var geoCity = [
        { 'Toronto', '43.6532', '-79.3831'},
        { 'Vancouver', '49.2612', '-123.1139'},
        { 'Calgary', '51.04532', '-114.0581'}
    ];
});
</script>

当输入城市字段时(实际上是在实际表单上使用bootstrap的typeahead),输入的值(在提交表单之前),需要用于搜索数组并提取匹配的纬度/经度值并输入它们到各自的隐藏输入字段中。

<input type="text" id="city" name="city" value="" />
<input type="hidden" id="latitude" name="latitude" value="" />
<input type="hidden" id="longitude" name="longitude" value="" />

希望有人能引导我朝着正确的方向前进。

【问题讨论】:

    标签: javascript jquery arrays forms extract


    【解决方案1】:

    是的,改变数据结构。然后将输入值与对象键相关联

    $(function () {
        var geoCity = {
            'Toronto': {
                'lat': '43.6532',
                'lon': '-79.3831'
            },
    
            'Vancouver': {
                'lat': '49.2612',
                'lon': '-123.1139'
            },
    
            'Calgary': {
                'lat': '51.04532',
                'lon': '-114.0581'
            }
        };
    
        $('#button').click(function () {
            var lat, lon, city = $('#city').val();
            if (city) {
                lat = geoCity[city].lat;
                lon = geoCity[city].lon;
                $('#latitude').val(lat);
                $('#longitude').val(lon);
            }
        });
    });
    

    Fiddle

    【讨论】:

    • 谢谢 Jeffman,我在上面提出的问题相同...如果要引用 90 多个城市,您会更改 geoCity 数组值的任何内容吗?
    • 不多。由于所有空间,它们看起来很麻烦,但可以删除。如果您将键和值放在“双”引号中,那么它将采用 JSON 格式。这将有助于在 PHP 或其他服务器环境中创建对象。
    • 谢谢杰夫曼,这会很好。我已经根据您的答案和 Chandu 的答案对决赛进行了调整。使用模糊功能,但如果没有输入城市,也将纬度/经度设置为空。这是最后的小提琴 - jsfiddle.net/swdmedia/ZYYYu/2
    【解决方案2】:

    您需要更改 geoCities 对象的结构,如下所述:

    $(function () {
        var geoCity = [{
            'city': 'Toronto',
            'location': ['43.6532', '-79.3831']
        }, {
            'city': 'Vancouver',
            'location': ['49.2612', '-123.1139']
        }, {
            'city': 'Calgary',
            'location': ['51.04532', '-114.0581']
        }];
    
        $("#city").blur(function () {
            $("#latitude").val("");
            $("#longitude").val("");
            var curCity = $("#city").val();
            var location = $.map(geoCity, function (itm) {
                if (itm.city === curCity) 
                    return itm.location; 
            });
            if(location) {
                $("#latitude").val(location[0]);
                $("#longitude").val(location[1]);
            }
        });
    });
    

    JsFiddle:http://jsfiddle.net/BJwhu/

    您可以$("#city").closest("form").submit 代替$("#city").blur

    【讨论】:

    • 谢谢 Chandu,如果要引用 90 多个城市,您会更改 geoCity 数组值的任何内容吗?
    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 2011-05-21
    相关资源
    最近更新 更多