如何在不点击jquery mobile中的任何按钮的情况下选择数据

时间:2014-01-03 12:20:21

标签: javascript php jquery html

代码如下

脚本

<script type="text/javascript">
    $(document).ready(function() {
        $("#like1").click(function() {
            $("#offer2").val(1);
            $("#like1").attr("src", "images/like_on.png");
            $("#unlike1").attr("src", "images/unlike_off.png");
        });
        $("#unlike1").click(function() {
            $("#offer2").val(2);
            $("#like1").attr("src", "images/like_off.png");
            $("#unlike1").attr("src", "images/unlike_on.png");
        });
        $("#like2").click(function() {
            $("#offer4").val(1);
            $("#like2").attr("src", "images/like_on.png");
            $("#unlike2").attr("src", "images/unlike_off.png");
        });
        $("#unlike2").click(function() {
            $("#offer4").val(2);
            $("#like2").attr("src", "images/like_off.png");
            $("#unlike2").attr("src", "images/unlike_on.png");
        });
        $("#update").click(function(e) {
            e.preventDefault();
            var video1 = $("#offer1").val();
            var rating1 = $("#offer2").val();
            var video2 = $("#offer3").val();
            var rating2 = $("#offer4").val();
            var dataString = 'video1=' + video1 + '&rating1=' + rating1 + '&video2=' + video2 + '&rating2=' + rating2;
            $.ajax({
                type: 'POST',
                data: dataString,
                url: 'insert.php',
                success: function(data) {
                    //alert(data);
                }
            });
        });
    });

</script>

<div id="content-area" >

    <br>
    <form name="frm" method="POST" action="">
        <div id="content-area1"> <p style="color:#FFFFFF;font-size:14px;margin-left:8px;padding-top:18px;word-spacing:5px;" class="content-videos">HOW TO PUT ON YOUR ACUVUE BRAND CONTACT LENSES</p></div>
        <div id="content-video1area"><a href="play_video1.html"><img src="images/play_video.png" width="57" height="53" style="float:right; margin-right: 278px;margin-top: 77px;"/></a></div>
        <div id="content-area2"><p style="color:#FFFFFF;font-size:14px;margin-left:15px;padding-top:22px;">HELPFUL?</p><img src="images/unlike_off.png" width="39" height="33" style="float:right; margin-right: 55px;margin-top: -23px;" id="unlike1"/><img src="images/like_off.png" width="39" height="33" style="float:right; margin-right: -83px;margin-top: -23px;" id="like1"/></div>
        <input type="hidden" name="offer1" id="offer1" value="1" />
        <input type="hidden" name="offer2" id="offer2" value="" />
        <br>
        <div id="content-area1"><p style="color:#FFFFFF;font-size:14px;margin-left:8px;padding-top:18px;word-spacing:5px;" class="content-videos">HOW TO TAKE OFF YOUR ACUVUE BRAND CONTACT LENSES</p> </div>
        <div id="content-video2area"><a href="play_video2.html"><img src="images/play_video.png" width="57" height="53" style="float:right; margin-right: 278px;margin-top: 77px;"/></a></div>
        <div id="content-area2"><p style="color:#FFFFFF;font-size:14px;margin-left:15px;padding-top:22px;">HELPFUL?</p><img src="images/unlike_off.png" width="39" height="33" style="float:right; margin-right: 55px;margin-top: -23px;" id="unlike2"/><img src="images/like_off.png" width="39" height="33" style="float:right; margin-right: -83px;margin-top: -23px;" id="like2"/></div>
        <input type="hidden" name="offer3" id="offer3" value="2" />
        <input type="hidden" name="offer4" id="offer4" value="" />


        <input type="hidden" name="name" id="name" value="" />
        <input type="hidden" name="last_name" id="last_name" value="" />
        <input type="submit" name="Update" id="update" value="Update" />
    </form>
</div>

php代码插入数据

<?php
include('libraries/config.php');
  $video1 = $_POST['video1'];
  $rating1 = $_POST['rating1'];
  $video2 = $_POST['video2'];
  $rating2 = $_POST['rating2'];
  $stmt = $db->prepare("INSERT INTO videos(videoID,rating) VALUES ('$video1','$rating1')");
        $stmt->execute();
  $stmt = $db->prepare("INSERT INTO videos(videoID,rating) VALUES ('$video2','$rating2')");
        $stmt->execute();       
?>

这里可以插入数据,但是当点击更新按钮时能够插入,必须自动插入数据而不点击任何按钮是否可能,如果有,请帮忙。我还需要在再次访问此页面时填充数据。谢谢

2 个答案:

答案 0 :(得分:0)

尝试卸载事件:

$(window).unload(function(){
    var myData = {'var1':42, 'arg2':24};// Data for myScript.php
    $.post("myScript.php", myData, function(data) {
        console.log(data);// Result of myScript.php
    });
});

编辑:在</script>之前添加此代码juste。此代码将调用您的更新功能。

$(window).unload(function(){
    $("#update").click({'preventDefault':function(){}});
});

答案 1 :(得分:0)

就像转换$(“#update”)一样简单。点击(function(e)

到命名函数

function update(){
     e.preventDefault();
        var video1 = $("#offer1").val(); //etc.

}

然后在每个“点击”功能中添加此内容。

$("#like1").click(function() {
        $("#offer2").val(1);
        $("#like1").attr("src", "images/like_on.png");
        $("#unlike1").attr("src", "images/unlike_off.png");
        update();
    });