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

时间:2019-02-14 03:16:13

标签: javascript

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

<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都获取未定义的数据。这里似乎有什么错误?

4 个答案:

答案 0 :(得分:1)

尝试获取该函数内部的元素。这是因为这两行

RewriteRule ^([^\.]+)/?$ index.php?/$1 [L]

其次,由于SELECT B.name from_city_name, C.name to_city_name FROM trip A INNER JOIN city B ON A.from_city=B.id INNER JOIN city C ON A.to_city=C.id; 类型为Submit,因此它是提交表单的默认行为

在选择提交按钮时不会更新

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

答案 1 :(得分:0)

new Thread(new Runnable() {
    @Override
    public void run() {
        // this run should be intercepted
    }
});

new Timer().scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        // this run should be ignored
    }
}, 0, 1000);

应该是

run()

答案 2 :(得分:0)

.text移至inpCountry。

<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);
}
</script>

答案 3 :(得分:0)

我希望这会对您有所帮助

function myNewFunction(){

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