gpt4 book ai didi

javascript - 从选定的选项中获取文本

转载 作者:行者123 更新时间:2023-11-28 08:41:31 25 4
gpt4 key购买 nike

当用户输入他们的地址详细信息时,我正在收集数据并创建一行来显示。我可以使用这样的行设置文本输入 var address1 = document.address.address1.value; ,但城镇是一个选择框,其值是 ID,文本是我需要的值去收集。对于文本输入,我使用 onkeyup="autoAddress();" ,对于选择,我使用 onchange="autoAddress();"

vartown = 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;
}

最佳答案

var town = document.address.town_id.options[document.address.town_id.selectedIndex].innerHTML;

关于javascript - 从选定的选项中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20421913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com