我试图创建一个包含大陆、地区、国家、省、城市、地区和村庄的链式下拉列表,但我被困在了country。
我的dropdown要求我使用数据库中的id来链接所有的dropdown,所以要获得文本名称,我通常使用以下方法:
国家/地区:
<select name="loc_country" class="loc_country loc5" id="loc_country" onchange="javacript: var valor2 = this.options[selectedIndex].text; document.getElementById('loc_country_real').value = valor2;">
<option value="0">-Select-</option>
</select>
<input type="text" id="loc_country_real" name="loc_country_real">
但是,这种方法这次不起作用,所以我尝试了另一种方法:
<select name="loc_country" class="loc_country loc5" id="loc_country">
<option value="0">-Select-</option>
</select>
<input type="text" id="loc_country_real" name="loc_country_real">
和js:
$("#continent").change(function () {
$("#loc_country_real").val($('#loc_country').text());
});
并希望当我的带有id=“大陆”的选择框改变时,值将会更新。
该方法也失败了,因为"loc_country_real“中给出的值与选择框"loc_country”不匹配。
(例如:当我选择欧洲大陆选择框时,loc_country选择框会给出一个欧洲国家列表,但"loc_country_real“中的值将是来自国家的亚洲)
我需要使"loc_country“和"loc_country_real”中的文本匹配,但我不知道如何做,请帮助。
发布于 2018-09-16 22:31:23
试试这个:
更改您的选择,使其具有与所需文本匹配的值,例如:
<select name="loc_country" class="loc_country loc5" id="loc_country">
<option value="0">--Select--</option>
<option value="USA">USA</option>
<option value="Canada">Canada</option>
<option value="Mexico">Mexico</option>
</select>
然后,可以使用document.getElementById('loc_country').value
获取选定的选项值
// Change `loc_country_real`'s value to match `loc_country` selected option
document.getElementById('loc_country_real').value = document.getElementById('loc_country').value;
或者使用jQuery的方式:
$('#loc_country').change(function(){
$('#loc_country_real').val($('#loc_country').val());
});
由于您提到不能使用:,因此不能使用值进行编辑
$('#loc_country').change(function(){
var textOfSelectedOption = $("#loc_country option:selected").text();
$('#loc_country_real').val(textOfSelectedOption);
});
https://stackoverflow.com/questions/52355066
复制相似问题