为什么我不能禁用我的按钮?

时间:2015-01-08 20:03:31

标签: javascript wordpress user-interface

我已经尝试了几个小时来在这个简短的脚本中禁用一个简单的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);
}

2 个答案:

答案 0 :(得分:1)

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

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;
}

希望这有帮助,

:)大卫

答案 1 :(得分:0)

好吧,有人暂时发布了一个低调的答案,说明我的按钮正在工作,但它缺乏CSS。这不是一个完全正确的答案,但似乎他有一些好点,因为这让我走上正轨。 (如果你正在读这篇文章,亲爱的前任回答者,重新发表你的答案,我会投票赞成它!)。

我真的不知道我在做什么,这里......我的代码结构肯定是对JS神灵的亵渎,但是它有效,所以这里有:

var sendButton;
var freezeButton;

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.disabled = true;
    sendButton.onclick = function store(){
        Webcam.snap( function(data_uri) {
            document.getElementById('my_result').innerHTML = '<img src="'+data_uri+'"/>';
        } );
        freezeButton.value = "Take snapshot!"
        sendButton.disabled = true;
    }
    placeHolder = document.getElementById("storeButton");
    placeHolder.appendChild(sendButton);
}

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