JavaScript 函数 this.title, this.value

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12244463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:30:12  来源:igfitidea点击:

Javascript function this.title, this.value

javascriptforms

提问by Codded

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.

我有一个 onchange 将值传递给 javascript 函数的表单。我已经设法通过了 this.value 和 student.value ,但是 this.title 没有返回任何东西。

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>

回答by rsplak

this.options[this.selectedIndex].title

This is the title of the selected option.

这是所选选项的标题。

Here is a JSFIDDLE.

这是一个JSFIDDLE

回答by Elias Van Ootegem

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.

这样,您就可以访问您需要的一切以及更多内容:您可以取消事件本身,并操纵其行为。

回答by Sandeep G B

Here is a sample (different example) http://jsfiddle.net/gbsandeep/Dk3nh/

这是一个示例(不同的示例) http://jsfiddle.net/gbsandeep/Dk3nh/

you can perform

你可以表演

mySelect.options[mySelect.selectedIndex].innerText

to get the value.

以获得价值。

回答by Vikdor

thisin 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.

this在那个 onchange 方法对应于 SELECT 对象而不是选定的 OPTION 对象。您应该掌握所选的 OPTION,然后访问 title 属性。

回答by Asciiom

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);