选定时更改选定的文本 [英] Change selected text when selected

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

问题描述

如何使用jQuery在选择框中更改所选文本?

How do you change the selected text in a select box using jQuery?

<select>
    <optgroup label="Name">
        <option>John</option>
        <option>Mike</option>
    </optgroup> 
</select>

即,当您选择John时.选择框将显示名称:John".取而代之的是约翰".

ie, when you select John. The select box would show, "Name: John". Instead, of just "John".

推荐答案

html:

<select id="myselect">
    <optgroup label="Name">
        <option>John</option>
        <option>Mike</option>
    </optgroup> 
</select>

js:

$("#myselect").change(function (event) {
    var o = $("#myselect option:selected")
    , v=o.text()
    , old = $("#myselect option:contains('name')")
    , oldv = old.html();

    oldv && old.html(oldv.replace('name: ',''));
    o.text('name: ' + v);
});

请参阅: http://jsfiddle.net/eQR2W/2/

这篇关于选定时更改选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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