This code:
$("#host_select").val($("#host_select option:first").val());
alert($("#host_select").val());
alerts null. But there are some options with values in host_select. What could be wrong?
This code:
$("#host_select").val($("#host_select option:first").val());
alert($("#host_select").val());
alerts null. But there are some options with values in host_select. What could be wrong?
Share Improve this question asked Feb 5, 2016 at 5:26 Vladimir AkkermanVladimir Akkerman 1313 silver badges14 bronze badges 5- code is huge. Everything works well except for this part. – Vladimir Akkerman Commented Feb 5, 2016 at 5:32
- just show your drop drown code .. – Amar Singh Commented Feb 5, 2016 at 5:34
- Data updated by json queries:<div class="col-md-3"> <select class="form-control" id="host_select"> </select> </div> – Vladimir Akkerman Commented Feb 5, 2016 at 5:36
- where are the options ??? @VladimirAkkerman – Amar Singh Commented Feb 5, 2016 at 5:46
- If you have options your code works fine: jsfiddle/n9sxvet4 – Valerio Gentile Commented Feb 5, 2016 at 5:57
3 Answers
Reset to default 4I guess, I found out. The thing is that you can't get selected value right after filling select input.
$('#location_select').change(function()
var sel=$("#location_select").val();
Here sel will be null. That's because at this moment browser still didn't make selection pletely. So I used setTimeout to wait selection to get pleted.
You can do more research about javascript and html
here is an example:
https://fiddle.jshell/v4rwxz1q/
html:
<select name="" id="host_select">
<option value="1">first</option>
<option value="2">second</option>
</select>
js:
var first = $("option:first", "#host_select").val();
alert(first)
wish can let you have a good start
<html>
<head>
<script src="http://code.jquery./jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
var newOptions = {
"Option 1": "value1",
"Option 2": "value2",
"Option 3": "value3"
};
var $el = $("#host_select");
$el.empty(); // remove old options
$.each(newOptions, function (value, key) {
$el.append($("<option></option>")
.attr("value", value).text(key));
});
// First
$("#host_select").val($("#host_select option:first").val());
alert($("#host_select").val());
//Second
$("#host_select").val($("#host_select option:eq(1)").val());
alert($("#host_select").val());
//third
$("#host_select").val($("#host_select option:eq(2)").val());
alert($("#host_select").val());
});
</script>
</head>
<body>
<select class="form-control" id="host_select"></select>
</body>
</html>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745249017a4618562.html
评论列表(0条)