Javascript或Jquery事件触发器 - 如何通过javascript将值传递到输入框

时间:2018-04-25 00:14:55

标签: javascript jquery

如何通过javascript将值传递到输入框

时触发事件
<!DOCTYPE html>
<html>
<body>

<p>Write something in the text field to trigger a function.</p>

<input type="text" id="myInput" oninput="myFunction()">

<p id="demo"></p>

<script>

document.getElementById("myInput").value = 100;

function myFunction() {
    var x = document.getElementById("myInput").value;
    document.getElementById("demo").innerHTML = "You wrote: " + x;
}
</script>

</body>
</html>

当值100自动生成时,&#34;你写了100&#34;应该显示

1 个答案:

答案 0 :(得分:0)

我真的不知道你的问题,这里的工作正常。 JS例子:

GetPaymentType
document.getElementById("myInput").value = 100;

function myFunction() {
    var x = document.getElementById("myInput").value;
    document.getElementById("demo").innerHTML = "You wrote: " + x;
}

jQuery示例:

<p>Write something in the text field to trigger a function.</p>

<input type="text" id="myInput" onkeyup="myFunction()">

<p id="demo">You wrote: 100</p>
$('#myInput').val(100);

$('#myInput').keyup(function(){
  $('#demo').html('You wrote: ' + $('#myInput').val());
});

我建议你使用onkeyup,因为它比onchange更具动态性。