dongsonghen9931 2012-11-21 21:53
浏览 41
已采纳

提交表单并使用ajax传递值

Index.html

function drawChart() {
                var jsonData = $.ajax({
                    url: "server.php?startdate=" + start_date + "&enddate=" + end_date + "&type=" type,
                    dataType: "json",
                    async: false
                }).responseText;

                var obj = jQuery.parseJSON(jsonData);
                var data = google.visualization.arrayToDataTable(obj);
    (...)

This vars: start_date, end_date and type should be obtained by a form without refreshing the page, so I can send it to server.php and do some stuff
How can I do that without change this jsonData structure etc ?
Because I need it to build charts.

Thanks one more time :) ps: Notice me if I wasnt clear :)

  • 写回答

2条回答 默认 最新

  • doumei3828 2012-11-21 22:09
    关注

    Imagining a form (such as the one below) on your page, some jQuery would allow you to grab the values entered into the text fields and store into javascript variables that you can then use in your drawChart() function.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function(){
            $('#never_name_a_button_submit').click(function() {
                var start_date = $('start_date').val();
                var end_date = $('end_date').val();
                var type = $('type').val();
            });
        });
    function drawChart() {
                var jsonData = $.ajax({
                    url: "server.php?startdate=" + start_date "&enddate=" + end_date + "&type=" type,
                    dataType: "json",
                    async: false
                }).responseText;
    
                var obj = jQuery.parseJSON(jsonData);
                var data = google.visualization.arrayToDataTable(obj);
    (...)
    
    </script>
    
    <form id="myForm">
        <input type="text" name="start_date" id="start_date"><br />
        <input type="text" name="end_date" id="end_date"><br />
        <input type="text" name="type" id="type"><br />
        <input type="button" id="never_name_a_button_submit">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程