将输入字段设置为标签元素的当前值 [英] Set input fields to the current value of label elements

查看:79
本文介绍了将输入字段设置为标签元素的当前值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取函数showEditionBlock()中标签元素的当前值并将它们设置为相应的输入字段?该函数将显示"edit" div,但输入未获取标签的原始值.

How can I get the current value of the label elements within function showEditionBlock() and set them to the corresponding input fields? The function will show the "edit" div, but the inputs are not getting the original values of the labels.

<html>
<head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>

</head>
<script>
$(document).ready(
    function()
        {

           $("#companyNameText").keyup(
           function()
           {
               $("#companyNameLabel").html(this.value);
           });

           $("#companyCountryText").keyup(
           function()
           {
               $("#companyCountryLabel").html(this.value);
           });
    });
function showEditionBlock(){
    //$("div#edit").css('display','block').fadeIn("slow");  
    $("div#edit").fadeIn('slow', function(){ 
            $(this).css('display', 'inline'); 
        }); 
    }

function hideEditionBlock(){
    //$("div#edit").css('display','block').fadeIn("slow");  
    $("div#edit").fadeOut('slow', function(){ 
            $(this).css('display', 'none'); 
        }); 
    }



</script>
        <body>
                <div id="preview">
            <fieldset>
                        <label id="companyNameLabel" class="workExperience">
                                This is my company
                        </label>
                        <label id="companyCountryLabel" class="workExperience">
                                This is my company Country
                        </label>
                        <input type="button" value="edit" onclick="showEditionBlock();"/>
            </fieldset>
                </div>
                <div id="edit" style="display:none;">
            <fieldset>
                        <label>Company Name: </label>
                        <input type="text" id="companyNameText" />
                        <label>Company Country: </label>
                        <input type="text" id="companyCountryText" />
                        <input type="button" value="Ok" onclick="hideEditionBlock();"/>
            </fieldset>
                </div>
        </body>
</html>

推荐答案

您应将标签的innerHTML分配给输入的value.在普通的Javascript中,您可以执行以下操作:

You should assign the innerHTML of the labels to the values of the inputs. In plain ol' Javascript you can do something like:

document.getElementById("companyNameText").value =
    document.getElementById("companyNameLabel").innerHTML;
document.getElementById("companyCountryText").value =
    document.getElementById("companyCountryLabel").innerHTML;

使用jQuery,这是相同的:

Using jQuery, this is the same:

$("#companyNameText").val($("#companyNameLabel").html());
$("#companyCountryText").val($("#companyCountryLabel").html());

这篇关于将输入字段设置为标签元素的当前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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