使用pageinit()而不是document.ready

时间:2013-04-22 13:26:10

标签: jquery-mobile

我尝试了很多方法将我的应用程序用作jquerymobile,但没有成功。 我知道我必须使用pageinit而不是文档准备好。你能告诉我正确的方法吗?当我添加它时,它根本就不起作用。这是代码

<head>
<link rel="stylesheet" href="https://s3.amazonaws.com/codiqa-cdn/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script>
    if (navigator.userAgent.match(/Android/i)) {
        window.scrollTo(0, 1);
    }
</script>
<script src="https://s3.amazonaws.com/codiqa-cdn/jquery-1.7.2.min.js"></script>
<script src="https://s3.amazonaws.com/codiqa-cdn/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script>
    try {
        $(function() {});
    } catch (error) {
        console.error("Your javascript has an error: " + error);
    }
</script>
<script>
    $(document).ready(function() {
        $("form").change(function() {
            $.post("test2.php",
            $("#myform").serialize());
        });
    });
</script>
</head>
<body>
    <div id="my-page" data-role="page">
        <form name="myform" id="myform" method="get">
            <label for="DogalGaz">Doğal Gaz</label>
            <select name="DogalGaz" id="DogalGaz" data-theme="" data-role="slider">
                <option value="">Seçiniz</option>
                <option value="off">Off</option>
                <option value="on">On</option>
            </select>
        </form>
    </div>

我所做的只是将表单数据发送到test2.php文件并进行处理。感谢您的帮助

1 个答案:

答案 0 :(得分:0)

Flip Toggle Switch应该有2个选项而不是3个。

试试这个:

<!DOCTYPE html>
<head>
<link rel="stylesheet" href="https://s3.amazonaws.com/codiqa-cdn/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script>
    if (navigator.userAgent.match(/Android/i)) {
        window.scrollTo(0, 1);
    }
</script>
<script src="https://s3.amazonaws.com/codiqa-cdn/jquery-1.7.2.min.js"></script>
<script src="https://s3.amazonaws.com/codiqa-cdn/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script>
    $(document).on('pageinit', function() {
        $("#myform").change(function() { // use #myform instead of form to only serialize one form.
            $.post("test2.php",
              $("#myform").serialize());
        });
    });
</script>
</head>
<body>
    <div id="my-page" data-role="page">
        <!-- include action and method in case javascript handler fails -->
        <form name="myform" id="myform" action="test2.php" method="post"> 
            <label for="DogalGaz">Doğal Gaz. Seçiniz: </label>
            <select name="DogalGaz" id="DogalGaz" data-theme="" data-role="slider">
                <option value="off">Off</option>
                <option value="on">On</option>
            </select>
        </form>
    </div>
</body>
</html>

每次切换切换时都会发布到test2.php。