现在有3个文本框:
displayName: <s:textfield name="displayName" id="displayName" readonly="true"/>
lastName : <s:textfield name="lastName" id="lastName" />
firstName : <s:textfield name="firstName" id="firstName" />其中displayName的值是 “lastName值+firstName值”,就是说当 lastName 或 firstName 有值的时候会严格按照
“lastName值+firstName值”的格式自动填充到displayName中去。而且,如果lastName 或 firstName的值有改变的时候,displayName中的值也会严格按照“lastName值+firstName值”的格式改变。自己尝试过使用jquery的change方法,但无法做到以上效果。也许是我考虑的思路不对。请问这样的效果使用jquery可以实现吗?该怎样做?还望各位指点。
displayName: <s:textfield name="displayName" id="displayName" readonly="true"/>
lastName : <s:textfield name="lastName" id="lastName" />
firstName : <s:textfield name="firstName" id="firstName" />其中displayName的值是 “lastName值+firstName值”,就是说当 lastName 或 firstName 有值的时候会严格按照
“lastName值+firstName值”的格式自动填充到displayName中去。而且,如果lastName 或 firstName的值有改变的时候,displayName中的值也会严格按照“lastName值+firstName值”的格式改变。自己尝试过使用jquery的change方法,但无法做到以上效果。也许是我考虑的思路不对。请问这样的效果使用jquery可以实现吗?该怎样做?还望各位指点。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
<script src="jquery-1.5.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#lastName").keyup(function(){
$("#displayName").val($("#firstName").val()+$(this).val());
});
$("#firstName").keyup(function(){
$("#displayName").val($(this).val()+$("#lastName").val());
});
});
</script>
</head>
<body>
displayName: <input name="displayName" id="displayName" readonly="readonly"/>
lastName : <input name="lastName" id="lastName" />
firstName : <input name="firstName" id="firstName" />
</body>
</html>
$(document).ready(function(){
$("#lastName,#firstName").bind("keyup",function(){
$("#displayName").val($("#firstName").val() +$("#lastName").val());
});
});这样可否 。
var displayName = $("#displayName"),
lastName = $("#lastName"),
firstName = $("#firstName");
lastName.change(function(){
displayName.val($(this).val() + firstName.val());
})
firstName.change(function(){
displayName.val(lastName.val() + $(this).val());
})
})
<!doctype html>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body>
displayName: <input name="displayName" id="displayName" />
lastName : <input name="lastName" id="lastName" />
firstName : <input name="firstName" id="firstName" />
<script>
function $(o){return document.getElementById(o)}
function change(){
$('displayName').value = $('lastName').value + ' ' + $('firstName').value
}
$('lastName').onkeyup = $('firstName').onkeyup = change;
</script>
</body>
</html>
楼主 这个意思?