gpt4 book ai didi

javascript - 在提交/页面刷新之前动态更改输入值?

转载 作者:行者123 更新时间:2023-11-28 05:05:32 25 4
gpt4 key购买 nike

我希望将隐藏的输入值设置为“0”或“1”,具体取决于用户是否填写电话号码字段。

这是我当前的代码:

<script>
if(document.getElementById('user-tel').value!='')
{
document.getElementById('send-text').value='1';
}
else {document.getElementById('send-text').value='0';}
</script>

但我需要他们保存两次隐藏的 #send-text 字段以在 sql 中更新。我怎样才能使这个隐藏字段在提交表单之前更新为 1,而不是在他们在电话字段中输入任何数字时更新为 1?

最佳答案

您可以使用表单 onsubmit 事件属性在 jQuery 中简单地实现此目的:

<form onsubmit="$('#send-text').val(($('#user-tel').val()!='')?'1':'0');">
<!-- Your form goes here -->
</form>

在此处了解更多信息:W3 Schools

关于javascript - 在提交/页面刷新之前动态更改输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784459/

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