设置输入字段标签要素的当前值(Set input fields to the current val

2019-09-28 19:25发布

我怎样才能得到函数内的标签要素的当前值showEditionBlock()并将其设置为相应的输入字段? 该功能将显示“编辑”分区,但投入没有得到标签的原始值。

<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>

Answer 1:

您应该分配innerHTML标签到的value的输入秒。 在纯醇”的Javascript,你可以这样做:

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

使用jQuery,这是相同的:

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


文章来源: Set input fields to the current value of label elements