拆分字符串,并分配给字段

时间:2014-03-21 09:44:51

标签: javascript jquery html split

我有一个相当简单的问题。当我的页面被加载时,它会生成一个代表并解决的长字符串,例如:

101, Dalmations Avenue, Miami, Florida, USA, 908343

使用jQuery,我可以使用:

拆分字符串
var address = sel.options[sel.selectedIndex].value;

var houseNumber = address.split(",")[0];
var street      = address.split(",")[1];
var district    = address.split(",")[2];
var county      = address.split(",")[3];
var country     = address.split(",")[4];
var postalcode  = address.split(",")[5];

然后我需要将拆分值设置为我的html中的字段,这些字段是onload,被指定为:

<input type="hidden" name="ADDR_HOUSENUMBER" value="">
<input type="hidden" name="ADDR_STREET" value="">
<input type="hidden" name="ADDR_DISTRICT" value="">
<input type="hidden" name="ADDR_COUNTY" value="">
<input type="hidden" name="ADDR_COUNTRY" value="">
<input type="hidden" name="ADDR_POSTALCODE" value="">

我的印象是我可以分配我的分割值,如:

document.forms[0].ADDR_HOUSENUMBER[01].value = houseNumber;

这是对的吗?

由于

4 个答案:

答案 0 :(得分:5)

尝试

var address = '101, Dalmations Avenue, Miami, Florida, USA, 908343'; //sel.options[sel.selectedIndex].value;

var $ins= $('input[type="hidden"][name^="ADDR"]');
$.each(address.split(/\s*,\s+/), function(i, val){
    $ins.eq(i).val(val)
})

演示:Fiddle

注意:这取决于输入字段的顺序,所以如果你改变它们会产生错误的结果


如果您不想依赖订单,那么

var address = '101, Dalmations Avenue, Miami, Florida, USA, 908343'; //sel.options[sel.selectedIndex].value;

var order = ["ADDR_HOUSENUMBER", "ADDR_STREET", "ADDR_DISTRICT", "ADDR_COUNTY", "ADDR_COUNTRY", "ADDR_POSTALCODE"];
$.each(address.split(/\s*,\s+/), function (i, val) {
    $('input[name="' + order[i] + '"]').val(val)
})

演示:Fiddle

答案 1 :(得分:1)

将class-attribute添加到你的html。

<input type="hidden" class="ADDR_HOUSENUMBER" name="ADDR_HOUSENUMBER" value="">
<input type="hidden" class="ADDR_STREET" name="ADDR_STREET" value="">
<input type="hidden" class="ADDR_DISTRICT" name="ADDR_DISTRICT" value="">
<input type="hidden" class="ADDR_COUNTY" name="ADDR_COUNTY" value="">
<input type="hidden" class="ADDR_COUNTRY" name="ADDR_COUNTRY" value="">
<input type="hidden" class="ADDR_POSTALCODE" name="ADDR_POSTALCODE" value="">

然后用

设置它们的值
$(".ADDR_HOUSENUMBER").val(houseNumber);

等...

答案 2 :(得分:1)

Using Javascript. Check this fiddle

HTML

<input type="text" name="ADDR_HOUSENUMBER" value="" />
<input type="text" name="ADDR_STREET" value="" />
<input type="text" name="ADDR_DISTRICT" value="" />
<input type="text" name="ADDR_COUNTY" value="" />
<input type="text" name="ADDR_COUNTRY" value="" />

JAVASCRIPT

var address = "101, Dalmations Avenue, Miami, Florida, USA, 908343";

var houseNumber = address.split(",")[0];
var street = address.split(",")[1];
var district = address.split(",")[2];
var county = address.split(",")[3];
var country = address.split(",")[4];
var postalcode = address.split(",")[5];

document.getElementsByName("ADDR_HOUSENUMBER")[0].value = houseNumber;

答案 3 :(得分:1)

每次都不要拆分字符串。拆分一次,稍后再使用。您可能需要以下内容:

var address = sel.options[sel.selectedIndex].value;
var vals = address.split(",");

var houseNumber = vals[0];
var street      = vals[1];
...
...


document.forms["yourFormName"].elements["ADDR_HOUSENUMBER"].value = houseNumber;
...
...