加载数据后,Jquery在大写的不间断单词之间添加空格

时间:2012-09-04 14:18:12

标签: jquery

<script type="text/javascript">
var CountryVal;
var StateVal;
$(document).ready(function(){
    $('#ItemCountry').change(function(){
        CountryVal = this.value; // value of the select element
        $("#StateFeeder").html('<img src="/images/mapCountry/AU/ajax-loader.gif" class="IMG-Loading" />');
        $('#StateFeeder').load('/StateFeeder/StateFeeder-Buy.html #StateFeeder-'+CountryVal, function(){
            $('#ItemState').change(function(){
                StateVal = this.value; // value of the select element
                $("#CityFeeder").html('<img src="/images/mapCountry/AU/ajax-loader.gif" class="IMG-Loading" />');
                $('#CityFeeder').load('/Country/'+CountryVal+'/_'+CountryVal+'_suburbs.html #Suburbs-'+StateVal, function(){
                    $('#CityFeeder select').attr('size', '5');
                    $('#CityFeeder select').change(function(){  
                        this.form['CAT_Custom_221342'].value=$(this).val();
                    }); 
                });        
            })
        });
    });
});           
</script>

上面的Jquery会将下拉菜单从其他页面加载到当前页面,并在用户进行选择时将值传递到另一个输入字段。问题是,从远程页面中的#StateFeeder获取的选项值包含以下格式:

<option value="Kabardino-BalkarRepublic">Kabardino-Balkar Republic</option>
<option value="Karachay-CherkessRepublic">Karachay-Cherkess Republic</option>
<option value="KareliaRepublic">Karelia Republic</option>
<option value="KomiRepublic">Komi Republic</option>
<option value="MariElRepublic">Mari El Republic</option>
<option value="MordoviaRepublic">Mordovia Republic</option>

上面的值是不间断的大写单词,我希望它们在下面的大写单词之间用空格格式化(plz保持“ - ”btw单词)当它们被加载到当前页面时,特别关注#ItemState部分:当城市值加载到#ItemState菜单中时,例如“KareliaRepublic”值(没有空格“KareliaRepublic”)被视为id,因此可以正确加载远程菜单,请格式化它直到此加载过程完成然后处理添加空间操作。非常感谢:< / p>

<option value="Kabardino-Balkar Republic">Kabardino-Balkar Republic</option>
<option value="Karachay-Cherkess Republic">Karachay-Cherkess Republic</option>
<option value="Karelia Republic">Karelia Republic</option>
<option value="Komi Republic">Komi Republic</option>
<option value="Mari El Republic">Mari El Republic</option>
<option value="Mordovia Republic">Mordovia Republic</option>

2 个答案:

答案 0 :(得分:1)

这应该有效:

value = value.​​replace(/([^-])([A-Z])/g, "$1 $2");

答案 1 :(得分:0)

使用正则表达式来操作值字符串:

var stateVal = this.value.replace(/([a-z])([A-Z])/g, '$1 $2');

http://jsfiddle.net/mblase75/BgUFm/

More on using regular expressions with .replace()