最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - convert string to integer of selected option - Stack Overflow

programmeradmin1浏览0评论

Hi I would like to convert the values of the selected options into integers. Unfortunately its not working.

Example HTML:

<label for="ausgangssprache">Ausgangssprache</label>
<select name="ausgangssprache" size="1" id="ausgangssprache"> <option value="0.78">Deutsch</option> <option value="0.78">Englisch</option> <option value="0.78" selected>Franz&ouml;sisch</option> <option value="0.78">Spanisch</option> <option value="0.78">Italienisch</option> <option value="1.1">Arabisch</option></select>

Javascript:

var a = document.getElementById("ausgangssprache");
var ausgangssprache = a.options[a.selectedIndex].value;

I've tried with parseInt() this both, without getting the selected value, when testing via alert()

var a = parseInt(document.getElementById("ausgangssprache"));
var ausgangssprache = a.options[a.selectedIndex].value;

var a = document.getElementById("ausgangssprache");
var ausgangssprache = parseInt(a.options[a.selectedIndex].value);

Hi I would like to convert the values of the selected options into integers. Unfortunately its not working.

Example HTML:

<label for="ausgangssprache">Ausgangssprache</label>
<select name="ausgangssprache" size="1" id="ausgangssprache"> <option value="0.78">Deutsch</option> <option value="0.78">Englisch</option> <option value="0.78" selected>Franz&ouml;sisch</option> <option value="0.78">Spanisch</option> <option value="0.78">Italienisch</option> <option value="1.1">Arabisch</option></select>

Javascript:

var a = document.getElementById("ausgangssprache");
var ausgangssprache = a.options[a.selectedIndex].value;

I've tried with parseInt() this both, without getting the selected value, when testing via alert()

var a = parseInt(document.getElementById("ausgangssprache"));
var ausgangssprache = a.options[a.selectedIndex].value;

var a = document.getElementById("ausgangssprache");
var ausgangssprache = parseInt(a.options[a.selectedIndex].value);
Share Improve this question edited Jul 19, 2022 at 18:27 aynber 23k9 gold badges54 silver badges68 bronze badges asked Mar 20, 2017 at 5:26 KrystianKrystian 9875 gold badges23 silver badges53 bronze badges 2
  • 2 Try Number(a.options[a.selectedIndex].value); – Rayon Commented Mar 20, 2017 at 5:27
  • 1 Possible duplicate of How do I convert a string into an integer in JavaScript? – Max Commented Mar 20, 2017 at 5:29
Add a ment  | 

1 Answer 1

Reset to default 4
var a = parseInt(document.getElementById("ausgangssprache").value);

you can use this, unless you're doing multiple select. to not use parseInt, assign value as a int like value=3 instead of value="3"

发布评论

评论列表(0)

  1. 暂无评论