未捕获的ReferenceError:未定义MyFunction - PhoneGap

时间:2013-05-21 14:15:36

标签: javascript cordova

我正在将PhoneGap和jQuery Mobile用于移动网络应用。当我尝试运行在onclick属性上设置的MyFunction时,我在控制台中出现错误

  

未捕获的ReferenceError:未定义MyFunction。

这是我的代码:

<!DOCTYPE html>
<html>
<head>
 <title>Product</title>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" href="css/jquery.mobile-1.3.1.min.css" />
<script type="text/javascript" src="cordova-2.7.0.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.3.1.min.js"></script>



<!--Generation Script Starts here -->
<script type="text/javascript">
function makeid()
{
    var text = "";
    var possible = "123456789";

    for( var i=0; i < 4; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

function makeid1()
{
    var text1 = "";
    var possible = "123456789";

    for( var i=0; i < 1; i++ )
        text1 += possible.charAt(Math.floor(Math.random() * possible.length));

    return text1;
}

function makeid2()
{
    var text2 = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for( var i=0; i < 1; i++ )
        text2 += possible.charAt(Math.floor(Math.random() * possible.length));

    return text2;
}

function makeid3()
{
    var text3 = "";
    var possible = "123456789";

    for( var i=0; i < 2; i++ )
        text3 += possible.charAt(Math.floor(Math.random() * possible.length));

    return text3;
}

function myFunction()
{
              document.getElementById("demo").innerHTML="D01E-BOA0-" + makeid() + "-" + makeid1() + makeid2() + makeid3();
}

</script>
<!-- Generation Script Ends Here -->

</head>
<body>

<div data-role="page">

<div data-role="header" data-theme="b">
    <a href="index.html" data-role="button" data-icon="home" data-iconpos="notext" data-theme="b" data-iconshadow="false" data-inline="true">Home</a>
    <h1>Product</h1>
</div><!-- /header -->

<div data-role="content">


    <p id="demo" style="text-align:center;"></p>

    <input type="button" onclick="myFunction()" value="Generate!" data-theme="b">
</div><!-- /content -->


</div><!-- /page -->

</body>
</html>

2 个答案:

答案 0 :(得分:1)

我不确定这会有所帮助,将所有代码打包成文档,并且 使用jQuery处理按钮的click

<input type="button" id="generate" value="Generate!" data-theme="b">


$('#generate').on('click', function () {
    $("#demo").html("D01E-BOA0-" + makeid() + "-" + makeid1() + makeid2() + makeid3());
});

完整代码:

$(function () {

    function makeid() {
        var text = "";
        var possible = "123456789";

        for( var i=0; i < 4; i++ ) {
            text += possible.charAt(Math.floor(Math.random() * possible.length));
        }

        return text;
    }

    function makeid1() {
        var text1 = "";
        var possible = "123456789";

        for( var i=0; i < 1; i++ ) {
            text1 += possible.charAt(Math.floor(Math.random() * possible.length));
        }

        return text1;
    }

    function makeid2() {
        var text2 = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        for( var i=0; i < 1; i++ ) {
            text2 += possible.charAt(Math.floor(Math.random() * possible.length));
        }

        return text2;
    }

    function makeid3() {
        var text3 = "";
        var possible = "123456789";

        for( var i=0; i < 2; i++ ) {
            text3 += possible.charAt(Math.floor(Math.random() * possible.length));
        }

        return text3;
    }

    $('#generate').on('click', function () {
        $("#demo").html("D01E-BOA0-" + makeid() + "-" + makeid1() + makeid2() + makeid3());
    });

});

检查此FIDDLE以在android上测试它。

答案 1 :(得分:1)

这可能会解决您的问题:

  1. 为您的身体添加onLoad的监听器:

    <body onLoad="fireItUp()">

  2. 使用被调用的方法为cordova的设备添加事件监听器:

    function fireItUp() {
    document.addEventListener("deviceready", onDeviceReady, false);
    
    }
    
  3. 使用回拨方法将您的活动绑定到jquery手机&#39;点击&#39; event(您可能希望为按钮指定id或类,因为这会绑定到所有输入元素,这可能是不受欢迎的):

    function onDeviceReady() {            
        jQuery(input).bind('tap',function(event){
        myFunction();
        });
    }