如何将自动完成地址添加到动态添加的输入字段

时间:2015-04-18 11:40:17

标签: javascript asp.net-mvc google-maps

我希望用户在动态添加的文本区域没有输入正确的Google地址(或从建议框中选择)时看到提醒。我该怎么做?

我尝试过以下代码 我希望在运行时添加新的divs时,自动完成方法应该应用于该输入字段,当用户单击提交按钮而不选择任何建议的地址时,它应该显示警告。
请帮助我,我的代码在哪里错了?

function updateNames() {

var rows = $(".clonedInput");
var index = 0;

rows.each(function () {
    var inputFields = $(this).find("input");
    inputFields.each(function () {
        var currentName = $(this).attr("name");
        
        $(this).removeClass("hasDatepicker");
        $(this).removeClass("hasTimepicker");
        
        $(this).attr("name", currentName.replace(/\[(.*?)\]/, '[' + index + ']'));
        
    });
    
    var textareaFields = $(this).find("textarea");
    textareaFields.each(function () {
        
        var currName = $(this).attr("name");
        $(this).attr("name", currName.replace(/\[(.*?)\]/, '[' + index + ']'));
        var places = new google.maps.places.Autocomplete(currName);
        var a = places.getPlace();
        if (a == null) {
            alert('Please Select a valid location');
            
        }
        else { }
        var currId = $(this).attr("id");
        $(this).attr("id", currId.replace(/\[(.*?)\]/, '[' + index + ']'));
        
    });
    
    var numberSpan = $(this).find("span.taskNumber");
    numberSpan.html(index + 1);
    
    index++;
    
});
}

0 个答案:

没有答案