admin管理员组文章数量:1221760
I have a form which onchange passes values to a javascript function. I have managed to get this.value and student.value passed but this.title does not return anything.
How would i go about getting the title of the option?
Thanks
<form>
<select id="student" style="width:100%">
<option value="">--</option>
<option value="">A</option>
</select>
<select id="course" onchange="showarchive(this.value, this.title, student.value)" style="width:100%"/>
<option value="">--</option>
<option value="" title="" class=""></option>
</select>
I have a form which onchange passes values to a javascript function. I have managed to get this.value and student.value passed but this.title does not return anything.
How would i go about getting the title of the option?
Thanks
<form>
<select id="student" style="width:100%">
<option value="">--</option>
<option value="">A</option>
</select>
<select id="course" onchange="showarchive(this.value, this.title, student.value)" style="width:100%"/>
<option value="">--</option>
<option value="" title="" class=""></option>
</select>
Share
Improve this question
asked Sep 3, 2012 at 8:35
CoddedCodded
1,25614 gold badges43 silver badges74 bronze badges
5 Answers
Reset to default 6this.options[this.selectedIndex].title
This is the title of the selected option.
Here is a JSFIDDLE.
Why go to all that trouble of typing all those arguments? everything you need can be gotten from the event object, just change:
<select id="course" onchange="showarchive(this.value, this.title, student.value)">
To:
<select id="course" onchange="showarchive(event)">
and change your function a bit:
function showarchive (e)
{
e = e || window.event;//just for safety
var select = e.target || e.srcElement;//select.id === course now
var selected = select.options[select.selectedIndex];//your freshly selected option element here
var title = selected.title;//etc...
//as an added bonus:
if (e.preventDefault)
{
e.preventDefault();
e.stopPropagation();
return e;
}
e.returnValue = false;
e.cancelBubble = true;//redundant: onchange doesn't bubble in IE
}
That way, you have access to everything you need and more: you can cancel the event itself, and manipulate its behaviour.
this
in that onchange method corresponds to the SELECT object and not the selected OPTION object. You should get hold of the selected OPTION and then access the title attribute.
You can get it something like this:
function getSelectedText(elementId) {
var elt = document.getElementById(elementId);
if (elt.selectedIndex == -1)
return null;
return elt.options[elt.selectedIndex].text;
}
and in onchange:
showarchive(this.value, getSelectedText('course'), student.value);
Here is a sample (different example) http://jsfiddle.net/gbsandeep/Dk3nh/
you can perform
mySelect.options[mySelect.selectedIndex].innerText
to get the value.
本文标签: formsJavascript function thistitlethisvalueStack Overflow
版权声明:本文标题:forms - Javascript function this.title, this.value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739271577a2155837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论