get value of selected <select> element in dart

浪尽此生 提交于 2019-12-23 17:55:38

问题


how i can obtain selected option of element? document.query("#id") - returns Element which hasn't value propery so i can't do:

var val = document.query("#id").value;

 

var val = (SelectElement)(document.query("#id")).value; // - "SelectElement is class and cannot be used as an expression"

how to do type conversion in dart correctly?


回答1:


In general, when dealing with any HTML element, I usually query the HTML element first:

InputElement myElement = document.query(#myInputElement);

Then use some event to capture the value contained in that element:

int val = Math.parseInt(myElement.value);

So for example, in HTML I could have an input element of type range:

<input id="mySlider" type="range" min="1000" max="10000" step="20" value="5000" />

In Dart, I would grab the value of this slider using a mouse up event:

InputElement mySlider = document.query('#mySlider');
mySlider.on.mouseUp.add((e) {
  int _val = Math.parseInt(mySlider.value);
  document.query('#status').innerHTML = "$_val";
});

Edit: In M1, you can now just use the cast operator as. If your HTML looked like this:

<p id="text">This is some text</p>

You could retrieve the text from the paragraph tag by doing this:

String str = (query('#text') as ParagraphElement).text;



回答2:


just need:

String str = document.query("#idOfSelect").value;

will be warning, but you can ignore it




回答3:


I used:

var val = int.parse(query("#id").value, onError: (_) => 1);

This prevents an exception in case query("#id").value is null and assigns a default value (1).



来源:https://stackoverflow.com/questions/10670173/get-value-of-selected-select-element-in-dart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!