选择SELECT值BY值? [英] Selecting a SELECT value BY value?

查看:100
本文介绍了选择SELECT值BY值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能非常简单,但我有一个SELECT框。每个选项的价值在

选择框中将是一个数字,例如...


< option value =" 5">测试条目< / option>

因此,我如何通过onClick更改Select <的选定选项

仅通过选择的值?我不能使用''selectedIndex'',因为它

与选项的价值无关。


谢谢!

解决方案

@sh写道:


可能很简单,但我选择框中的每个选项的值将是一个数字,例如......

< option value =" 5">测试条目< / option>

因此,如何通过onClick更改选择的选项
选择仅通过选择的值?我不能使用''selectedIndex''
因为它与选项的价值无关。


是的,它确实与价值有关。

它实际上是选定指数的点,这比

更有用值,因为可以为不同的选项找到相同的值!

因此,使用它指向选择框中的右侧OPTION,并检索

值或文本。


示例:


< form name =" fakeform">

< select name = "例子" onClick =" getVal();">

< option value =" 1"> One

< option value =" 2"> ; 2

< option value =" 3"> Three

< option value =" 1">还有一个

< / select>

< / form>


< script type =" text / javascript">

function getVal(){

//获取所选索引的索引

var selInd = document.forms [" fakeform"]。example.selectedIndex;

var associatedValue =

document.forms [" fakeform"]。example [selInd] .value;

var associatedText = document.forms [ " fakeform"]。example [selInd] .text;

//用它做一些有用的事情


alert(" selInd =" + selInd +" \ nncorrespondingValue =" + cor responsesValue +" \ nncorrespondingText =" + Correspondi ngText);

}

< / script>


问候,
Erwin Moller


谢谢!




Erwin Moller写道:


更正:

< select name =" example" onClick =" getVal();">




顺便说一句:onClick不行。

它应该是onChange( )。


感谢您的回复,我想我明白你的意思,虽然我是

试图设置所选的选项通过页面上另一个

元素的onClick进行SELECT?我想你误解了我的问题?

Erwin Moller

< si **************** ************************** @ spam yourself.com>写在

消息新闻:44 *********************** @ news.xs4all.nl ...

@sh写道:


可能很简单,但我有一个SELECT框中每个选项的值
选择框将是一个数字,例如...

< option value =" 5">一个测试条目< / option>

因此,怎么做我,通过onClick,更改选择的选项
选择仅通过选择的值?我不能使用''selectedIndex''
因为它与选项的价值无关。



是的,它确实与价值有关。
它实际上是选定索引的POINTING,它比值更有用,因为可以为不同的选项找到相同的值!
所以用它指向正确的OPTION in选择框,并检索
值或文本。

示例:

< form name =" fakeform">
< select名称= QUOT;例子" onClick =" getVal();">
< option value =" 1"> One
< option value =" 2"> Two
< option value =" 3"> Three
< option value =" 1">还有一个
< / select>
< / form>

< script type =" text / javascript">
function getVal(){
//获取所选的索引
var selInd = document.forms [" ; fakeform"]。example.selectedIndex;
var_allatedValue =
document.forms [" fakeform"]。example [selInd] .value;
var associatedText = document.forms ["伪造形式"]。例子[selInd] .text;
//用它做一些有用的事情

警告(" selInd =" + selInd +" \\\
correspondingValue =" + corresponmentValue +" \ nncorrespondingText =" + Correspondi ngText);
}
< / script>

问候,
Erwin Moller


谢谢!



Probably very simple, but I have a SELECT box The value of each option in
the Select box will be a number, for instance...

<option value="5">A test entry</option>

Therefore, how do I, via an onClick, change the selected option of a Select
via the Value of the select only? I can''t use the ''selectedIndex'' because it
doesn''t relate to the value of the option.

Thanks!

解决方案

@sh wrote:
Hi

Probably very simple, but I have a SELECT box The value of each option in
the Select box will be a number, for instance...

<option value="5">A test entry</option>

Therefore, how do I, via an onClick, change the selected option of a
Select via the Value of the select only? I can''t use the ''selectedIndex''
because it doesn''t relate to the value of the option.
Yes it does relate to the value.
It is actually POINTING to the selected index, which is more usefull than
the value, since the same value can be found for different options!
So use it to point to the right OPTION in the selectbox, and retrieve the
value or text.

Example:

<form name="fakeform">
<select name="example" onClick="getVal();">
<option value="1">One
<option value="2">Two
<option value="3">Three
<option value="1">Also One
</select>
</form>

<script type="text/javascript">
function getVal(){
// get the index of the selected one
var selInd = document.forms["fakeform"].example.selectedIndex;
var correspondingValue =
document.forms["fakeform"].example[selInd].value;
var correspondingText = document.forms["fakeform"].example[selInd].text;
// do something usefull with it

alert("selInd="+selInd+"\ncorrespondingValue="+cor respondingValue+"\ncorrespondingText="+correspondi ngText);
}
</script>

Regards,
Erwin Moller


Thanks!




Erwin Moller wrote:

Correction:

<select name="example" onClick="getVal();">



By the way: onClick is not OK.
It should be onChange().


Thanks for your reply, I think I understand what you mean, although I''m
trying to set the selected option in the SELECT via an onClick from another
element on the page? I think you''ve misunderstood my question?
"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:44***********************@news.xs4all.nl...

@sh wrote:
Hi

Probably very simple, but I have a SELECT box The value of each option in
the Select box will be a number, for instance...

<option value="5">A test entry</option>

Therefore, how do I, via an onClick, change the selected option of a
Select via the Value of the select only? I can''t use the ''selectedIndex''
because it doesn''t relate to the value of the option.



Yes it does relate to the value.
It is actually POINTING to the selected index, which is more usefull than
the value, since the same value can be found for different options!
So use it to point to the right OPTION in the selectbox, and retrieve the
value or text.

Example:

<form name="fakeform">
<select name="example" onClick="getVal();">
<option value="1">One
<option value="2">Two
<option value="3">Three
<option value="1">Also One
</select>
</form>

<script type="text/javascript">
function getVal(){
// get the index of the selected one
var selInd = document.forms["fakeform"].example.selectedIndex;
var correspondingValue =
document.forms["fakeform"].example[selInd].value;
var correspondingText = document.forms["fakeform"].example[selInd].text;
// do something usefull with it

alert("selInd="+selInd+"\ncorrespondingValue="+cor respondingValue+"\ncorrespondingText="+correspondi ngText);
}
</script>

Regards,
Erwin Moller


Thanks!



这篇关于选择SELECT值BY值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆