当我单击lname
字段时,我想自动将fname
,mname
和fullname
的值复制并合并到fullname
中。
这是HTML:
<input type="text" class="form-control" id="lname">
<input type="text" class="form-control" id="fname">
<input type="text" class="form-control" id="mname">
<input type="text" class="form-control" id="fullname" onclick="namefunc()">
Javascript:
<script type="text/javascript">
$(document).ready(function () {
function namefunc() {
var n1 = $('#fname');
var n2 = $('#mname');
var n3 = $('#lname');
var fn = $('#fullname');
fn.val(n1.val() + " " + n2.val() + " " + n3.val());
}
});
</script>
它不起作用。请帮忙!
答案 0 :(得分:1)
就像@Nicolay在评论中说的那样,我的函数不需要在doc ready处理程序中,它可以直接进入script标记内。我尝试过:
<script type="text/javascript">
function namefunc() {
var n1 = $('#fname');
var n2 = $('#mname');
var n3 = $('#lname');
var fn = $('#fullname');
fn.val(n1.val() + " " + n2.val() + " " + n3.val());
};
</script>
它奏效了! :)