Javascript 将输入字段设置为标签元素的当前值
如何获取函数Javascript 将输入字段设置为标签元素的当前值,javascript,jquery,input,label,Javascript,Jquery,Input,Label,如何获取函数showEditionBlock()中标签元素的当前值并将其设置为相应的输入字段?该函数将显示“编辑”div,但输入不会获得标签的原始值 <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> </head> <s
showEditionBlock()
中标签元素的当前值并将其设置为相应的输入字段?该函数将显示“编辑”div,但输入不会获得标签的原始值
<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>
$(文件)。准备好了吗(
函数()
{
$(“#公司名称”).keyup(
函数()
{
$(“#companyNameLabel”).html(this.value);
});
$(“#公司CountryText”).keyup(
函数()
{
$(“#companyCountryLabel”).html(this.value);
});
});
函数showEditionBlock(){
//$(“div#edit”).css('display','block').fadeIn(“slow”);
$(“div#edit”).fadeIn('slow',function(){
$(this.css('display','inline');
});
}
函数hideEditionBlock(){
//$(“div#edit”).css('display','block').fadeIn(“slow”);
$(“div#edit”).fadeOut('slow',function(){
$(this.css('display','none');
});
}
这是我的公司
这是我的公司所在的国家
公司名称:
公司所在国:
您应该将标签的innerHTML
分配给输入的值。在纯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());
您应该将标签的innerHTML
分配给输入的值。在纯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());