gpt4 book ai didi

javascript - 为什么我无法禁用按钮?

转载 作者:行者123 更新时间:2023-12-03 11:06:09 25 4
gpt4 key购买 nike

我花了几个小时尝试在这个简短的脚本中禁用一个简单的 javascript 按钮 (id="storeButton")。我已经尝试了在互联网上找到的各种语法变体来实现这一效果,但无济于事。我正在 WordPress 页面中执行此操作。有人可以告诉我我的错误是什么吗?

WordPress 页面:

<p style="text-align: center;">Welcome!</p>
<strong>What is this project, and why contribute?</strong>

text

&nbsp;

<script src="/scripts/alert.js" type="text/javascript"></script>
<script src="/webcamjs/webcam.js" type="text/javascript"></script>
<script src="/scripts/take_snapshot.js" type="text/javascript"></script>

<div class="centre" id="my_camera" style="width:320px; height:240px;"></div>
<br>
<div id="freezeButton"></div>
<div id="storeButton"></div>
<br>
<div id="my_result"></div>

<script type="text/javascript">
<!--
ShowAlert();
webcamConfigure();
attach();
createFreezeButton();
disableBtn();
createStoreButton();
//-->
</script>

take_snapshot.js,问题似乎出在此处

document.getElementById("storeButton").disabled = true;:

function webcamConfigure(){
Webcam.set({
width: 320,
height: 240,
dest_width: 640,
dest_height: 480,
image_format: 'jpeg',
jpeg_quality: 100,
force_flash: true
});
}

function attach(){
Webcam.attach( '#my_camera' );
}

function createStoreButton(){
sendButton = document.createElement("input");
sendButton.type = "button";
sendButton.value = "Send snapshot!";
sendButton.onclick = function store(){
Webcam.snap( function(data_uri) {
document.getElementById('my_result').innerHTML = '<img src="'+data_uri+'"/>';
} );
}
placeHolder = document.getElementById("storeButton");
placeHolder.appendChild(sendButton);
}

function disableBtn(){
document.getElementById("storeButton").disabled = true;
}

function createFreezeButton(){
myButton = document.createElement("input");
myButton.type = "button";
myButton.value = "Take snapshot!";
myButton.onclick = function freeze(){
if (myButton.value=="Take snapshot!") {
myButton.value = "Discard snapshot!";
Webcam.freeze();
//document.getElementById("storeButton").disabled = false;
}
else {
myButton.value = "Take snapshot!";
Webcam.unfreeze();
//document.getElementById("storeButton").disabled = true;
}
}
placeHolder2 = document.getElementById("freezeButton");
placeHolder2.appendChild(myButton);
}

最佳答案

您正在尝试禁用名为“storeButton”的 div,而不是禁用您创建的包含在该 div 内的按钮。在您的上下文中,一种简单的方法是在创建按钮时使用公共(public)变量,当您想要禁用它时可以使用该变量:

var sendButton;
function webcamConfigure(){
Webcam.set({
width: 320,
height: 240,
dest_width: 640,
dest_height: 480,
image_format: 'jpeg',
jpeg_quality: 100,
force_flash: true
});
}

function attach(){
Webcam.attach( '#my_camera' );
}

function createStoreButton(){
sendButton = document.createElement("input");
sendButton.type = "button";
sendButton.value = "Send snapshot!";
sendButton.onclick = function store(){
Webcam.snap( function(data_uri) {
document.getElementById('my_result').innerHTML = '<img src="'+data_uri+'"/>';
} );
}
placeHolder = document.getElementById("storeButton");
placeHolder.appendChild(sendButton);
}

function disableBtn(){
sendButton.disabled = true;
}

希望这有帮助,

:) 大卫

关于javascript - 为什么我无法禁用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848599/

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