Symfony2 Ajax发送选项选择值

时间:2016-04-04 12:13:03

标签: php ajax symfony

我将脚本发送到控制器时出现问题选择的选项为null。

Jquery代码是: (当用户选择状态时,运行脚本。

            $('.status_select').on({
            change: function(e) {

                e.preventDefault();
                var $href1 = $(this).attr('id');


                $('<div></div>').load($href1+' form', function(){

                    var $form1 = $(this).children('form');
                    var $cb1 = $form1.find('select[name="marcin_adminbundle_updatezamtype[status]"]');

                    $cb1.prop('value');

                    var $url1 = $form1.attr('action');
                    var $data1 = $form1.serialize();

                    $.ajax({
                        url: $url1,
                        data: $data1,
                        method: 'post',
                        dataType: 'json',
                        cache: false,
                        success: function(obj) {
                            var $tic = $('#task-complete-'+obj.id+' .status_select');
                            if(obj.complete){


                            }
                            else {

                            $('#wynik'+obj.id).load(document.URL + ' #wynik'+obj.id);
                            $('#wynik_b'+obj.id).load(document.URL + ' #wynik_b'+obj.id);
                            }
                        },
                        complete: function() {
                            console.log('complete!');

                        },
                        error: function(err) {
                            console.log(err);
                        }
                    });
                });
            }
        });
        });

请帮忙 我希望在选择optopn时发送一个值。

HTML:

<tr><th id="task-complete-{{ article.id }}">{{ article.id }}</th> 
    <th>{{ article.jakie_zam }}</th>
    <th><select name="marcin_adminbundle_updatezamype[status]" id="{{ path('marcin_admin_dashboard_update', {'id': article.id }) }}" class="status_select" href="{{ path('marcin_admin_dashboard_update', {'id': article.id }) }}"><option value="wersja robocza"{% if article.status == 'wersja robocza' %} selected="selected" {% endif %}>wersja robocza</option><option value="przesłane do realizacji"{% if article.status == 'przesłane do realizacji' %} selected="selected" {% endif %}>przesłane do realizacji</option><option value="przyjęte do realizacji"{% if article.status == 'przyjęte do realizacji' %} selected="selected" {% endif %}>przyjęte do realizacji</option><option value="w realizacji"{% if article.status == 'w realizacji' %} selected="selected" {% endif %}>w realizacji</option><option value="zrealizowane"{% if article.status == 'zrealizowane' %} selected="selected" {% endif %}>zrealizowane</option><option value="odebrane"{% if article.status == 'odebrane' %} selected="selected" {% endif %}>odebrane</option></select></th>
        </tr>

控制台日志:

panel:829 Objectabort: (a)always: ()complete: ()done: ()error: ()fail: ()getAllResponseHeaders: ()getResponseHeader: (a)overrideMimeType: (a)pipe: ()progress: ()promise: (a)readyState: 4responseText: "<!DOCTYPE html>↵<html>↵    <head>↵        <meta charset="UTF-8" />↵        <meta http-equiv="X-UA-Compatible" content="IE=edge">↵        <title>Administracja GM</title>↵        <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">↵                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">↵         <!-- Ionicons -->↵        <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">↵        <link rel="stylesheet" type="text/css" href="/bundles/marcinadmin/bootstrap/css/bootstrap.min.css" />↵        <link rel="stylesheet" type="text/css" href="/bundles/marcinadmin/dist/css/AdminLTE.min.css" />↵        <link rel="stylesheet" type="text/css" href="/bundles/marcinadmin/dist/css/skins/skin-blue.min.css" />↵        <link rel="stylesheet" type="text/css" href="/bundles/marcinadmin/plugins/datatables/dataTables.bootstrap.css" />↵        ↵         <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->↵        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->↵        <!--[if lt IE 9]>↵            <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>↵            <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>↵        <![endif]-->↵        ↵        <link rel="icon" type="image/x-icon" href="/favicon.ico" />↵    </head>↵    <body class="hold-transition skin-blue sidebar-mini">↵    <div class="wrapper">↵↵      <!-- Main Header -->↵      <header class="main-header">↵↵        <!-- Logo -->↵        <a href="/app_dev.php/panel" class="logo">↵          <!-- mini logo for sidebar mini 50x50 pixels -->↵          <span class="logo-mini"><b>GM</b></span>↵          <!-- logo for regular state and mobile devices -->↵          <span class="logo-lg">Grupa<b>MAGNUM</b></span>↵        </a>↵↵        <!-- Header Navbar -->↵        <nav class="navbar navbar-static-top" role="navigation">↵          <!-- Sidebar toggle button-->↵          <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">↵            <span class="sr-only">Toggle navigation</span>↵          </a>↵          <!-- Navbar Right Menu -->↵          <div class="navbar-custom-menu">↵            <ul class="nav navbar-nav">↵              <!-- Messages: style can be found in dropdown.less-->↵              <!-- /.messages-menu -->↵↵              <!-- Notifications Menu -->↵              <!-- Tasks Menu -->↵              <!-- User Account Menu -->↵              <li class="dropdown user user-menu">↵                <!-- Menu Toggle Button -->↵                <a href="#" class="dropdown-toggle" data-toggle="dropdown">↵                  <!-- The user image in the navbar-->↵                  <img src="https://sklep.grupamagnum.eu//upload/GM_fb.jpg" class="user-image" alt="User Image">↵                  <!-- hidden-xs hides the username on small devices so only the image appears. -->↵                  <span class="hidden-xs">Admin GM</span>↵                </a>↵                <ul class="dropdown-menu">↵                  <!-- The user image in the menu -->↵                  <li class="user-header">↵                    <img src="https://sklep.grupamagnum.eu//upload/GM_fb.jpg" class="img-circle" alt="User Image">↵                    <p>↵                      Administrator GrupaMAGNUM↵                      <small>Rok założenia 1996</small>↵                    </p>↵                  </li>↵                  <!-- Menu Body ↵                  <li class="user-body">↵                    <div class="col-xs-4 text-center">↵                      <a href="#">Followers</a>↵                    </div>↵                    <div class="col-xs-4 text-center">↵                      <a href="#">Sales</a>↵                    </div>↵                    <div class="col-xs-4 text-center">↵                      <a href="#">Friends</a>↵                    </div>↵                  </li>-->↵                  <!-- Menu Footer-->↵                  <li class="user-footer">↵                   <!-- <div class="pull-left">↵                      <a href="#" class="btn btn-default btn-flat">Profile</a>↵                    </div>-->↵                    <div class="pull-right">↵                      <a href="/app_dev.php/logout" class="btn btn-default btn-flat">Wyloguj</a>↵                    </div>↵                  </li>↵                </ul>↵              </li>↵              <!-- Control Sidebar Toggle Button -->↵              <li>↵                <a href="#" data-toggle="control-sidebar"><i class="fa fa-gears"></i></a>↵              </li>↵            </ul>↵          </div>↵        </nav>↵      </header>↵      <!-- Left side column. contains the logo and sidebar -->↵      <aside class="main-sidebar">↵↵        <!-- sidebar: style can be found in sidebar.less -->↵        <section class="sidebar">↵↵          <!-- Sidebar user panel (optional) -->↵          <…avascript" src="/bundles/marcinadmin/plugins/jQuery/jQuery-2.1.4.min.js"></script>↵        <script type="text/javascript" src="/bundles/marcinadmin/bootstrap/js/bootstrap.min.js"></script>↵        <script type="text/javascript" src="/bundles/marcinadmin/dist/js/app.min.js"></script>↵        <!-- DataTables -->↵        <script type="text/javascript" src="/bundles/marcinadmin/plugins/datatables/jquery.dataTables.min.js"></script>↵        <script type="text/javascript" src="/bundles/marcinadmin/plugins/datatables/dataTables.bootstrap.min.js"></script>↵        ↵    <script>↵        $(function(){↵            $('.ajax-task-complete').on({↵                click: function(e) {↵                    e.preventDefault();↵                    var $href = $(this).attr('href');↵                    ↵                    $('<div></div>').load($href+' form', function(){↵                        var $form = $(this).children('form');↵                        var $cb = $form.find('input[type="checkbox"]');↵                        ↵                        $cb.prop('checked', !$cb.prop('checked'));↵                        ↵                        var $cb1 = $form.find('select[name="marcin_adminbundle_updatezamype[status]"]');↵                        ↵                        $cb1.prop('selected', !$cb1.prop('selected'));↵                        ↵                        var $url = $form.attr('action');↵                        var $data = $form.serialize();↵                        ↵                        $.ajax({↵                            url: $url,↵                            data: $data,↵                            method: 'post',↵                            dataType: 'json',↵                            cache: false,↵                            success: function(obj) {↵                                var $tic = $('#task-complete-'+obj.id+' .ajax-task-complete');↵                                if(obj.complete){↵                                    //$tic.text('X');↵                                    ↵                                }↵                                else {↵                                    //$tic.text('0');↵                                $('#wynik'+obj.id).load(document.URL + ' #wynik'+obj.id);↵                                $('#wynik_b'+obj.id).load(document.URL + ' #wynik_b'+obj.id);↵                                }↵                            },↵                            complete: function() {↵                                console.log('complete!');↵↵                            },↵                            error: function(err) {↵                                console.log(err);↵                            }↵                        });↵                    });↵                }↵            });↵            ↵            ↵            $('.status_select').on({↵                change: function(e) {↵           //$(document).on('change', '.status_select', function() {↵          //  $('select[name*="status_select"]').prop('selected', true).change(function(e){↵                    e.preventDefault();↵                    var $href = $(this).attr('id');↵                   ↵                    ↵                    $('<div></div>').load($href+' form', function(){↵                        //var $form = $(this).children('form');↵                        var $form =  $(this).closest('form');;↵                        var $cb = $form.find('select[name="marcin_adminbundle_updatezamtype[status]"]');↵                        ↵                        $cb.prop('selected');↵                        ↵                        var $url = $form.attr('action');↵                        var $data = $form.serialize();↵                        ↵                        $.ajax({↵                            url: $url,↵                            data: $data,↵                            method: 'post',↵                            dataType: 'json',↵                            cache: false,↵                            success: function(obj) {↵                                var $tic = $('#task-complete-'+obj.id+' .status_select');↵                                if(obj.complete){↵                                    //$tic.text('X');↵                                    ↵                                }↵                                else {↵                                    //$tic.text('0');↵                                $('#wynik'+obj.id).load(document.URL + ' #wynik'+obj.id);↵                                $('#wynik_b'+obj.id).load(document.URL + ' #wynik_b'+obj.id);↵                                }↵                            },↵                            complete: function() {↵                                console.log('complete!');↵↵                            },↵                            error: function(err) {↵                                console.log(err);↵                            }↵                        });↵                    });↵                }↵            });↵            });↵        ↵        </script>↵    </body>↵</html>↵"setRequestHeader: (a,b)state: ()status: 200statusCode: (a)statusText: "OK"success: ()then: ()__proto__: Object

小组:825完成!

1 个答案:

答案 0 :(得分:0)

我认为$form1未定义。

你应该通过$(this).closest('form');得到它,假设你的选择框实际上是在html表单标记之间。

你应该避免对JS变量使用“$”并将其保留为PHP部分。

我不确定第一个加载函数的相关性:

 $('<div></div>').load($href1+' form', function(){});