当用户输入他们的地址详细信息时,我正在收集数据并创建一行显示。我可以使用类似var address1 = document.address.address1.value;
的行设置文本输入,但是城镇是一个选择框,其值为ID,文本是我需要收集的值。对于文本输入我使用onkeyup =“autoAddress();”和我使用onchange =“autoAddress();”
第var town = document.address.town_id.value
行返回ID,但我需要文字
<input type="text" name="address1" id="address1" data-rule-required="true" class="form-control" value="{$form_data.address1}" onkeyup="autoAddress();">
<input type="text" name="address2" id="address2" class="form-control" value="{$form_data.address2}" onkeyup="autoAddress();">
<select class="form-control chosen" name="town_id" id="town_id" data-rule-required="true" data-placeholder="Choose a town" onchange="autoAddress();">
<option value=""></option>
{foreach $towns as $town}
<option value="{$town.id}" {if $town.id eq $form_data.town_id}selected="selected"{/if}>{$town.town_name} {if $town.region_name}, {$town.region_name}{/if}</option>
{/foreach}
</select>
function autoAddress(){
var house_number = document.address.house_number.value;
var address1 = document.address.address1.value;
var address2 = document.address.address2.value;
var town = document.address.town_id.value;
var postcode = document.address.postcode.value;
}
答案 0 :(得分:0)
var town = document.address.town_id.options[document.address.town_id.selectedIndex].innerHTML;
答案 1 :(得分:0)
你应该试试这个。
function autoAddress(){
var house_number = document.address.house_number.value;
var address1 = document.address.address1.value;
var address2 = document.address.address2.value;
var town = document.address.town_id.options[document.address.town_id.selectedIndex].text;
var postcode = document.address.postcode.value;
}
答案 2 :(得分:0)
您可以尝试这样的事情(Example)
HTML:将我添加onchange
的{{1}}通知更改为函数的参数
this
<强> JS:强>
<select onchange="autoAddress(this);"></select>
答案 3 :(得分:0)
尝试'.text'代替'.value'。 E.g:
var townElement = document.getElementById('town_id');
var townText = townElement[townElement.selectIndex].text;
答案 4 :(得分:0)
试试这个:
document.getElementById('town_id').options[document.getElementById('town_id').selectedIndex].text;