gpt4 book ai didi

javascript - 输入字段在收到值后不刷新

转载 作者:可可西里 更新时间:2023-11-01 13:21:30 26 4
gpt4 key购买 nike

从第一个输入字段收集数据并传送到第二个输入字段 2 的整个过程运行良好,但为了使值实际进入第二个输入字段,我必须从第一个输入字段中删除一些字母并重新- 输入一些数字,以便该值实际进入第二个输入。

怀疑的GIF。 enter image description here

    $(document).ready(function () {
$(function () {
var $cepAddressRouteTransporterGoogleMaps = $('#postal_code');
var $cepAddressRouteTransporter = $('#cepAddressRouteTransporter');

function onChange() {
$cepAddressRouteTransporter.val($cepAddressRouteTransporterGoogleMaps.val());
};

$('#postal_code')
.change(onChange)
.keyup(onChange);
});
});

第一个输入代表这个字段postal_code & 第二个输入代表这个字段cepAddressRouteTransporter

<form action="transporter/route" method="post" role="form">
<table id="address">
<tr>
<td class="label">Zip code</td>
<td class="wideField">
<input class="field" id="postal_code" name="postal_code">
</td>
</tr>
</table>
<div class="input-field col s6">
<i class="material-icons prefix">directions</i>
<input placeholder="Ex: 18214-780" id="cepAddressRouteTransporter" name="cepAddressRouteTransporter" type="text" class="validate">
<label for="cepAddressRouteTransporter">CEP:</label>
</div>
</form>

感谢您的帮助!

最佳答案

当 DOM 准备好时,你必须调用你的函数 onChange

onChange();

当您更新值时,触发 DOM 事件。

$('#postal_code').trigger('change');

$(function() {
var $cepAddressRouteTransporterGoogleMaps = $('#postal_code');
var $cepAddressRouteTransporter = $('#cepAddressRouteTransporter');

function onChange() {
$cepAddressRouteTransporter.val($cepAddressRouteTransporterGoogleMaps.val());
};

$('#postal_code').change(onChange).keyup(onChange);

// fire as soon as DOM is ready
onChange();

var t = window.setInterval(function(){
var n = Math.floor(Math.random() * 100000);
$('#postal_code').val(n);

// when you update the value, trigger the DOM event
$('#postal_code').trigger('change');
},1000);

});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
</head>
<body>

<form action="transporter/route" method="post" role="form">
<table id="address">
<tr>
<td class="label">Zip code</td>
<td class="wideField">
<input class="field" id="postal_code" name="postal_code" value="90210">
</td>
</tr>
</table>
<div class="input-field col s6">
<i class="material-icons prefix">directions</i>
<input placeholder="Ex: 18214-780" id="cepAddressRouteTransporter" name="cepAddressRouteTransporter" type="text" class="validate">
<label for="cepAddressRouteTransporter">CEP:</label>
</div>

</form>

</body>
</html>

关于javascript - 输入字段在收到值后不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46748991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com