通过单击下拉列表更改文本值-JQuery [英] Change text value by clicking drop down list - JQuery

查看:70
本文介绍了通过单击下拉列表更改文本值-JQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://jsfiddle.net/ZbcVH/1/

HTML

<div id="locationselect"><ul><li>United Kingdom</li><li>France</li><li>Germany</li></ul></div>
<input tpye="text" id="location" />

jQuery

$('#locationselect').click(function() {
    $('#location').html($(this).val());
});

如上面的链接所示,我基本上想从无序列表项中选择值,即 id ="locationselect" ,然后将所选列表项的值放入 id = 位置" 文本框.我不太确定我要去哪里错了.

As demonstrated in the link above, I basically want to select the value from the unordered list items, id="locationselect" and then put the selected list item's value into the id="location" text box. I'm not too sure where I'm going wrong.

我确定这很简单.我尝试查看其他问题以寻求帮助,但似乎没有点击.

I'm sure it's something simple. I've tried looking at other questions to help, but it doesn't seem to be clicking.

感谢您的帮助!

推荐答案

尝试此代码.在输入中,使用val()更改值.并在您的li上找到text().

Try this code. In the input, use val() to change the value. And get text() on your li.

jsFiddle此处

$('document').ready(function(){
    $('#locationselect li').click(function() {
        $('#location').val($(this).text());
    });
});

或使用.on

$('document').ready(function(){
    $('#locationselect li').on('click',function() {
        $('#location').val($(this).text());
    });
});

这篇关于通过单击下拉列表更改文本值-JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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