[英]I can't get the value of a input within a datalist?
I have a datalist box that looks like this: 我有一个看起来像这样的数据列表框:
<td>
<input list="screens.screenid-datalist" type="text" id="screens.screenid" onblur="validate('0','0','jacques')">
<datalist id="screens.screenid-datalist">
<option value="Login"></option>
<option value="ScreenCreator"></option>
</datalist>
<label id="val-screens.screenid" class="Label_Error" style="visibility: hidden;">*</label>
</td>
and I have a JavaScript code which has to get the value from this datalist. 而且我有一个JavaScript代码,该代码必须从此数据列表中获取值。
I tried all the following things to get the value 我尝试了以下所有方法来获得价值
document.getElementById('screens.screenid').value
document.getElementById('screens.screenid').text
document.getElementById('screens.screenid').innerHTML
document.getElementById('screens.screenid').option
and it just does not seem to work. 而且似乎不起作用。
Is there something wrong with my JavaScript or with my HTML code? 我的JavaScript或HTML代码有问题吗?
when I use the console to get the value: 当我使用控制台获取值时:
Took sometime to figure this out. 花了一些时间解决这个问题。 Look at my code below :
看下面我的代码:
function validate(){ console.log(document.getElementById('screens.screenid').value); //WORKS console.log(document.getElementById('screens.screenid').text); console.log(document.getElementById('screens.screenid').innerHTML); console.log(document.getElementById('screens.screenid').option); }
<input list="screens.screenid-datalist" type="text" id="screens.screenid" onblur="validate('0','0','jacques')"> <datalist id="screens.screenid-datalist"> <option value="Login"></option> <option value="ScreenCreator"></option> </datalist> <label id="val-screens.screenid" class="Label_Error" style="visibility: hidden;">*</label> <a href='#' onclick="validate">validate</a>
Now the first one will get the value
as expected, but the text
option
will not work for the obvious reason that you do not have text here. 现在第一个将获得预期的
value
,但是text
option
由于明显的原因(此处没有文本)而无法使用。 Also, innerHTML
will get you the child html and not the value. 另外,
innerHTML
将使您获得子html,而不是其值。 Further more , you can't get innerHTML
of an input list, but you can get it for the datalist. 此外,您无法获取输入列表的
innerHTML
,但可以将其用于数据列表。
Try this : console.log(document.getElementById('screens.screenid-datalist').innerHTML);
试试这个:
console.log(document.getElementById('screens.screenid-datalist').innerHTML);
I tried it and got the innerHTML
without any hassle : 我尝试了一下,并获得了
innerHTML
没有任何麻烦:
<option value="Login"></option>
<option value="ScreenCreator"></option>
Find the bin here : http://jsbin.com/inigaj/1/edit 在此处找到垃圾桶: http : //jsbin.com/inigaj/1/edit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.