jQuery find and replace with arrays

后端 未结 3 2161

I need to search an input\'s value for all street abbreviations and replace with appropriate suffix. This is what I have so far:

jQuery(\'#colCenterAddress\'         


        
3条回答
  •  太阳男子
    2020-12-19 07:07

    One way to do this, is to loop through the val string, and if you see a word in the f array, replace it with its counterpart in the r array.

    jQuery('#colCenterAddress').val(function(i,val) {
        var f = ['Rd','St','Ave'];
        var r = ['Road','Street','Avenue'];
        var valArray = val.split(' ');
        $.each(valArray, function(i,v){
           var inF = $.inArray(v, f);
           if(inF !== -1){
             valArray[i] = v.replace(f[inF], r[inF]);
           }
        });
        return valArray.join(' ');
    });
    

提交回复
热议问题