我需要帮助动态更新表单提交的值作为用户类型输入字段。
<form>
<input type="text" name="number" id="number" value = 0 />
<input type="submit" id="submit">
</form>
<script>
var amount = 100;
var number = document.getElementById('number').value;
var total = amount + number;
var a = document.getElementById("submit");
a.value = "Pay $" + total;
</script>
现在,提交的价值是&#34;支付100&#34;。但是当用户在数字字段中输入4时,我希望它说&#34;支付104&#34;当他输入4.我也希望用户删除并输入不同的数字,并相应地更改值。我怎么做到这一点?提前谢谢。
答案 0 :(得分:1)
试试这个..
document.getElementById('number').addEventListener("input", function(){
if(this.value.length && !isNaN(parseInt(this.value))){
var total = amount + parseInt(this.value);
submit.value = "Pay $"+ total;
}
}, false);
var amount = 100;
var submit = document.getElementById("submit");
document.getElementById('number').addEventListener("input", function(){
if(this.value.length && !isNaN(parseInt(this.value))){
var total = amount + parseInt(this.value);
submit.value = "Pay $"+ total;
}
}, false);
<form>
<input type="text" name="number" id="number" value = 0 />
<input type="submit" id="submit">
</form>