使用Javascript从下拉列表中获取未定义的值/文本

使用Javascript从下拉列表中获取未定义的值/文本,javascript,Javascript,当我从下拉列表中选择项时,我不明白为什么会得到未定义或空数据 <form action=""> <select class="form-control regist-user-input" id="user.attributes.countryId" name="user.attributes.countryId" required> <option value="">Country</option&g

当我从下拉列表中选择项时,我不明白为什么会得到未定义或空数据

<form action="">
  <select class="form-control regist-user-input" id="user.attributes.countryId" name="user.attributes.countryId" required>
                        <option value="">Country</option>
                            <option value="1" >
                            AFGHANISTAN
                            </option>
                            <option value="2" >
                            ALAND ISLAND
                            </option>
                            <option value="3" >
                            ALBANIA
                            </option>
                            <option value="4" >
                            ALGERIA
                            </option>
                            <option value="5" >
                            AMERICAN SAMOA
                            </option>
                            <option value="6" >
                            ANDORRA
                            </option>
                            <option value="7" >
                            ANGOLA
                            </option>
                            <option value="8" >
                            ANGUILLA
                            </option>
                            <option value="9" >
                            ANTARCTICA
                            </option>
                            <option value="10" >
                            ANTIGUA AND BARBUDA
                            </option>
                    </select>
  <br><br>
  <input type="submit" onClick="myNewFunction()">
</form>

<script>
var e = document.getElementById('user.attributes.countryId');
var inpCountry = e.options[e.selectedIndex].text;

function myNewFunction() {
    alert(inpCountry.text);
}
</script>

inpCountry.value和inpCountry.text在尝试向其发出警报时,都会获取未定义的数据。这里出现了什么错误?

尝试获取函数中的元素。这是因为这两条线

var e=document.getElementById'user.attributes.countryId'; var inpCountry=e.options[e.selectedIndex].text

其次,由于输入类型是submit,因此它会阻止提交表单的默认行为

选择“提交”按钮时不会更新

函数MyNewFunction{ e、 防止违约 var e=document.getElementById'user.attributes.countryId'; var inpCountry=e.options[e.selectedIndex]; console.loginpCountry.innerHTML.trim; } 国 阿富汗 阿兰岛 阿尔巴尼亚 阿尔及利亚 美属萨摩亚 安道尔 安哥拉 安圭拉 南极洲 安提瓜和巴布达 应该是

function myNewFunction() {
    var inpCountry = e.options[e.selectedIndex].text; //so that selectedIndex is checked again whenever this function runs
    alert(inpCountry.text);
}
删除。将文本添加到输入国家/地区

国 阿富汗 阿兰岛 阿尔巴尼亚 阿尔及利亚 美属萨摩亚 安道尔 安哥拉 安圭拉 南极洲 安提瓜和巴布达 var e=document.getElementById'user.attributes.countryId'; var inpCountry=e.options[e.selectedIndex].text; 函数myNewFunction{ 在该国; }
我希望这会对你有所帮助

函数myNewFunction{

var e = document.getElementById('user.attributes.countryId');
var inpCountry = e.options[e.selectedIndex].text; 
alert(inpCountry);
}

只需将.text删除到您的alertinpCountry.text;这是所有我已经尝试过的,我得到的是country,但不完全是我所选项目的文本仍然未定义我已经尝试过,我得到的是country,但不完全是我所选项目的文本
var e = document.getElementById('user.attributes.countryId');
var inpCountry = e.options[e.selectedIndex].text; 
alert(inpCountry);
}