开发者

Change value of input onchange?

I am trying to create a simple JavaScript function. When someone inserts a number in an input field, the value of another field should change to that value. Here is what I have at the moment:

function updateInput(ish) {  
    fieldname.value = ish;  
}  
<input type="text" name=&q开发者_如何学Cuot;fieldname" id="fieldname" />  
<input type="text" name="thingy" onchange="updateInput(value)" /> 

Somehow this does not work, can someone help me out?


You can't access your fieldname as a global variable. Use document.getElementById:

function updateInput(ish){
    document.getElementById("fieldname").value = ish;
}

and

onchange="updateInput(this.value)"


for jQuery we can use below:

by input name:

$('input[name="textboxname"]').val('some value');

by input class:

$('input[type=text].textboxclass').val('some value');

by input id:

$('#textboxid').val('some value');


<input type="text" name="fieldname" id="fieldtobechanged" />  
<input type="text" name="thingy"  id="inputfield" />

I have used following code and it works instantly without any delay.

var timeoutID = null;
    
function findMember(str) {
    document.getElementById("fieldname").innerHTML = str;
}

$('#inputfield').keyup(function(e){
    clearTimeout(timeoutID);
    timeoutID = setTimeout(findMember.bind(undefined, e.target.value), 500);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜