如何在php中打印功能结果?

时间:2018-04-24 23:11:48

标签: javascript function

我有这段代码并尝试打印结果但是没有打印,请帮忙。三江源。



function GetRandom() {
        var myElement = document.getElementById("pwbx")
	var myArray = ['item 1', 'item 2']; 
	var item = myArray[(Math.random()*myArray.length)|0];
        myElement.value = (item);
}

<!DOCTYPE html>
<html>
    <body>
        <p> click the button to run the fumction</p>
        <button onclick="GetRandom()">Try it</button>
        <p id="pwbx"></p>
    </body>
</html>
&#13;
&#13;
&#13;

它适用于我使用输入表单类型来获取结果,但我不想要它,我希望它只是作为页面中的普通文本回显。谢谢。我是这个东西的菜鸟,所以寻求帮助。

&#13;
&#13;
   

 function GetRandom() {
            var myElement = document.getElementById("pwbx")
    	    var myArray = ['item 1', 'item 2']; 
    	    var item = myArray[(Math.random()*myArray.length)|0];
            myElement.value = (item);
 }
&#13;
    <!DOCTYPE html>
<html>
    <body>
        <p>Click the button to display a random number between 1 and 10.</p>
        <button onclick="GetRandom()">Try it</button>
        <input name="test" type="text" id="pwbx" value="">
        <p id="pwbx"></p>
    </body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

<p>代码没有value属性。请改用innerHTML

&#13;
&#13;
function GetRandom()
    {
        var myElement = document.getElementById("pwbx")
		var myArray = ['item 1', 'item 2']; 
		var item = myArray[(Math.random()*myArray.length)|0];
        myElement.innerHTML = item;
    }
&#13;
<!DOCTYPE html>
<html>
<body>

<p>Click the button to display a random number between 1 and 10.</p>

<button onclick="GetRandom()">Try it</button>


<p id="pwbx"></p>


</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

正如蒂姆在评论中所说的那样......你会想要innerHTML而不是value用于非输入标签....

<!DOCTYPE html>
<html>

    <body>
        <p> click the button to run the fumction</p>
        <button onclick="GetRandom()">Try it</button>
        <p id="pwbx"></p>
    </body>

    <script>
        var myArray = ['item 1', 'item 2']; //should move this outside of getRandom so it doesn't reallocate each run;

        function GetRandom() {    
            var randomInt = Math.floor(Math.random() * Math.floor(myArray.length));    
            var item = myArray[randomInt];

            document.getElementById("pwbx").innerHTML = item;
        }
    </script>
</html>