简体   繁体   English

选择一个选择框值时如何显示消息?

[英]How to display a message when a selectbox value is select?

For instance , a selectbox has 3 options 例如,一个选择框具有3个选项

a, b,c a,b,c

each one has a message assign 每个人都有一个消息分配

a : message for a a:一条消息

b : message for b b:b的消息

c : message for c c:c的消息

What i would like to achieve is when the optiton select , the message will be display next to the selectbox, when the optiton value change, the message will be changed at the same time. 我想实现的是,当optiton select时,该消息将显示在选择框旁边,当optiton值更改时,该消息将同时更改。 The messages are stored in an array 消息存储在数组中

Thank you. 谢谢。

Here is the link of a selectbox, it is not simple selectbox though, as there are three option in each three select box, when a option is selected , that option in the rest of the selectbox will be disabled 这是一个选择框的链接,但这并不是简单的选择框,因为每个三个选择框中都有三个选项,当选择一个选项时,其余选择框中的该选项将被禁用

http://jsfiddle.net/davidThomas/M33N9/ http://jsfiddle.net/davidThomas/M33N9/

Proof: 证明:

http://jsfiddle.net/iambriansreed/G7Lvx/ http://jsfiddle.net/iambriansreed/G7Lvx/

HTML: HTML:

<select>
    <option>a</option>
    <option>b</option>
    <option>c</option>
</select>
​<div id="message"></div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

JavaScript: JavaScript:

var messages = {
    a: 'Armadillo',
    b: 'Bear',
    c: 'Cougar'
}
$('select').change(function() {
    $('#message').text(messages[$(this).val()]);
});​

Change to this 改成这个

(var i = 0, len = elem.length; i < len; i++)

from this: 由此:

(var i = 0, len = sels.length; i < len; i++)

change your select box to have as value the position of message in your array for example when it shows A, then in change event you can get the message 更改选择框以将消息在数组中的位置作为值,例如当它显示A时,则在更改事件中,您可以获得消息

$('Yourselect').change(function(){
var msg = YourArray[$(this).val()];
// Show it wherever yu want
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM