我试图启用输入字段使用鼠标点击,有办法实现它吗?
我的界面是这样的:
注意:它需要禁用第一个输入字段,通过单击特定的输入文本,然后应该启用它。
我想启用一旦特定的输入字段被点击。
<?php foreach($grades as $grade): ?>
<td> <input type="text" class="form-control" value="<?php echo $grade['grade'] ?>" id="gradeid" disabled></td>
<?php endforeach; ?>
剧本:
<script>
const inputField = document.getElementById('gradeid');
inputField.onfocus = () => {
$('#gradeid').attr('disabled', 'disabled'\'');
};
</script>
发布于 2022-08-11 22:05:06
当按钮被禁用时,onclick
和onfocus
事件将无法工作。但是,您可以将事件添加到保存它的元素中。
例如,下面是HTML:
<table>
<tr>
<td onclick = "tdclicked(this)">
<input type="text" class="form-control" value="<?php echo $grade['grade'] ?>" id="gradeid1" disabled>
</td>
<td onclick = "tdclicked(this)">
<input type="text" class="form-control" value="<?php echo $grade['grade'] ?>" id="gradeid2" disabled>
</td>
<td onclick = "tdclicked(this)">
<input type="text" class="form-control" value="<?php echo $grade['grade'] ?>" id="gradeid3" disabled>
</td>
</tr>
</table>
这是Javascript。
function tdclicked(td) {
for (var i = 0; i < document.getElementsByClassName("form-control").length; i++) {
document.getElementsByClassName("form-control")[i].setAttribute("disabled", "true");
}
inputField = td.children[0];
inputField.removeAttribute("disabled");
}
发布于 2022-08-11 22:27:54
浏览器禁用禁用元素上的事件。当您要在多个字段上执行某些操作时,必须对元素或动态id元素进行分类。这里是工作演示,希望这能帮助你理解,
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
function buttonclicked(e) {
$('.gradeid').attr('disabled', 'true');
$(e).children("input").removeAttr("disabled");
}
</script>
</head>
<body>
<table>
<tr>
<td onclick = "buttonclicked(this)">
<input type="text" class="form-control gradeid" value="0" id="gradeid" disabled>
</td>
</tr>
<tr>
<td onclick = "buttonclicked(this)">
<input type="text" class="form-control gradeid" value="0" id="gradeid" disabled>
</td>
</tr>
<tr>
<td onclick = "buttonclicked(this)">
<input type="text" class="form-control gradeid" value="0" id="gradeid" disabled>
</td>
</tr>
<tr>
<td onclick = "buttonclicked(this)">
<input type="text" class="form-control gradeid" value="0" id="gradeid" disabled>
</td>
</tr>
</table
</body>
</html>
https://stackoverflow.com/questions/73329622
复制相似问题