gpt4 book ai didi

html - 一起使用 required 和 readonly 属性

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:05 24 4
gpt4 key购买 nike

我正在创建一个 HTML5 网络表单。输入之一用于地理定位,通过单击按钮(它插入 gps 坐标)来填充。由于此输入必须是必需的并且必须是只读的(我不希望用户在其中写入自定义数据),因此如何处理?HTML5 不允许使用 requiredreadonly (这是有道理的),但在我的例子中我需要应用它。您知道任何解决方法吗?

我在 SO 和 Google 上搜索和搜索这里,但似乎没有人知道解决方案。

这是我的部分代码:

    <form class="form-horizontal" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label class="control-label col-xs-3" for="coord">GPS coordinates:*</label>
<div class="col-xs-4">
<input type="text" class="form-control" id="coord" name="coord" required placeholder="Please enable geolocation service">
</div>
<div class="col-xs-3">
<button type="button" onclick="getLocation()" class="btn btn-primary" id="locate" disabled>Locate me</button>
</div>
</div>

最佳答案

使用event.preventDefault();以防止默认行为。

一定不能是 keydown,只需将它挂到您要点击的事件中...

不要忘记将“事件”添加到函数调用中,这样就可以了。

<input type="text" class="readonly" required />

<script>
$(".readonly").keydown(function(e){
e.preventDefault();
});
</script>

关于html - 一起使用 required 和 readonly 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29923355/

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