将多个值插入隐藏字段

将多个值插入隐藏字段

问题描述:

我有一个选择列表,您可以在其中选择多个城市-选择一个城市时,我想将邮政编码添加到隐藏字段中,现在我已将解决方案的值插入了隐藏字段,但是覆盖了该值如果是fx:点击一个新城市-它应该只附加到值上,例如:value ="value1 | value2 | value3"

I have a select list, where you can choose multiple cities - When choosing a city, i wanna add the Zipcode to a hidden field, the solution i have now inserts the value to the hidden field, however, it overwrites the value when fx: clicking a new city - It should just append to the value, like: value="value1|value2|value3"

 $jq('.select-cities').click(function () {
    var zipcodeValue = $jq(this).val();
    //alert(zipcodeValue);
    $jq('.hiddenFieldChosenAreas').val(zipcodeValue);
    $jq('.select-cities option:selected').appendTo('.chosen-cities');
});

尝试一下:

$jq('.select-cities').click(function () {
    var zipcodeValue = $jq(this).val();
    //alert(zipcodeValue);
    var oldVal = $jq('.hiddenFieldChosenAreas').val();
    $jq('.hiddenFieldChosenAreas').val(oldVal+"|"+ zipcodeValue);
    $jq('.select-cities option:selected').appendTo('.chosen-cities');
});