1

这是我正在谈论的演示 - http://jsfiddle.net/MatthewKosloski/qLpT9/

如果单击了“Foo”,并且在输入中输入了一个数字,并且单击了“发送”,我想执行代码。

<h1>Foo</h1>

<input type="text" id="amount" placeholder="Enter in a number."/>
<button id="send">Send</button>

我很确定我想太多了,我很感激在如此简洁的问题上提供的帮助。

4

4 回答 4

0

根据您的陈述并做出一些假设,请尝试以下方式:

(这会执行两次功能 - 当文本更改或单击按钮时)。

HTML:

<h1 id="">Foo</h1>

<input type="text" id="amount" placeholder="Enter in a number."/>

<button id="sendBtn">send</button>

JS:

document.getElementById("amount").addEventListener("change",poorRatingCalculation);
document.getElementById("sendBtn").addEventListener("click",poorRatingCalculation);

function poorRatingCalculation() {

    var rating = document.getElementById("amount").value;

    if(rating=="poor")  alert("Poor Service");
}

演示:http: //jsfiddle.net/wTqEv/

于 2013-07-14T02:49:46.000 回答
0

一个更好的、独立的例子:

http://jsfiddle.net/qLpT9/7/

(function()
{
    var clicked = false;

    var header = document.getElementById("header");
    var amount = document.getElementById("amount");
    var send = document.getElementById("send");

    header.addEventListener("click", function()
    {
        clicked = true;
    });

    send.addEventListener("click", function()
    {
        if(!clicked)
        {
            return
        }

        // Foo has been clicked

        var value = amount.value;

        console.log(value;)
    });

})();
于 2013-07-14T02:56:30.163 回答
0

试试这个:jfiddle 链接

var send = document.getElementById("send");
var h1 = document.getElementsByTagName("h1");

var foo_clicked = 0;

h1[0].onclick = function(){foo_clicked += 1; };

send.onclick = function(){
    if(document.getElementById("amount").value !='' && foo_clicked >0 )
    alert ('poor rating');

};
于 2013-07-14T03:21:14.993 回答
-1

这是你要找的吗?

http://jsfiddle.net/qLpT9/5/

function poorRatingCalculation(){
        if(myInput.value) {
            alert(myInput.value);
        }
 }

        var foo = document.getElementById("foo"),
            myInput = document.getElementById("amount");

  foo.addEventListener("click", poorRatingCalculation, false)
于 2013-07-14T02:47:45.400 回答