如何使用jQuery将标签的值获取到文本框中 [英] How to get the value of a label into a textbox with jQuery

查看:432
本文介绍了如何使用jQuery将标签的值获取到文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与此类似:如何获取文本框使用jquery 将值添加到标签中,但是我试图做相反的事情:我正在尝试将标签的值添加到文本框中.我认为切换代码中的元素很简单,但事实并非如此.我也确实查看了标题下方可能已经有您的答案的问题"部分中提出的一些问题,但没有找到对我有帮助的东西(也许解决方案是其中之一,但我只是不明白...).

My question is similar to this: How to get textbox value into label using jquery, but I'm trying to do opposite: I'm trying to get the value of a label into a into a textbox. I thought it'd be a simple matter of switching the elements in the code, but that's not the case, apparently. I also did take a look at some of the questions presented in the "Questions that may already have your answer" section below my title, but didn't find something that helped me (maybe the solution was in one of them, but I just didn't understand it...).

这是我桌子的html:

Here's my table's html:

<table id="tblBranchDetails">
    <tr>
        <td width="120px">Branch:</td>
            <td id="branchName" class="branchData">
                <label id="lblBranchName"></label>
                <input type="text" id="txtBranchName" />
            </td>...

正如以上文章的作者所指出的,这是行不通的:

As the author of the post above noted, this doesn't work:

$('input#hdnBranchName').val() = $('label#lblBranchName').text();

我已经尝试过这些:

$('input#txtBranchName').html($('label#lblBranchName').val());
$('input#txtBranchName').text($('label#lblBranchName').val());

这些都不起作用.所以我试图看看是否我没有正确选择文本框:

Neither of those worked. So I tried to see if maybe I wasn't selecting the textbox correctly:

$('table#tblBranchDetails input#txtBranchName').html($('label#lblBranchName').val());
$('table#tblBranchDetails input#txtBranchName').text($('label#lblBranchName').val());

但是这些都不起作用.

我该如何做,以及作为一个更多的知识:为什么我认为是显而易见的方法不起作用?

How do I do this, and as a matter of learning more: why doesn't what I would assume to be the obvious method work?

谢谢!

推荐答案

查看 docs ..

与要设置为选中/选中状态的每个匹配元素的值相对应的文本字符串或字符串数​​组. 此方法通常用于设置表单字段的值.

A string of text or an array of strings corresponding to the value of each matched element to set as selected/checked. This method is typically used to set the values of form fields.

尝试

$('input#hdnBranchName').val($('label#lblBranchName').text());

val()用于获取选定的输入值...在其中设置val('').这会将选定的输入字段设置为空

val() is to get the selected input value... where as val('') is to set.. this sets empty to the selected input field

这篇关于如何使用jQuery将标签的值获取到文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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