繁体   English   中英

jQuery获取文本框值并传递给控制器​​操作

[英]jquery to take textbox value and pass to controller action

我在窗体中有两个文本框。

当我在文本框中键入一些值并单击按钮时,我想在函数中回显此值。

我想利用我的文本框的价值,并通过使用javascript和ajax在单击按钮时在我的控件功能中传递它。

但我的脚本无法正常工作,也没有显示任何值。

 <script>
    $(document).ready(function() {
        $("#subvessels").on('click', function() {
            var shipment_title = $("#shipment_title").val(); //textbox value 
            var shipment_point = $("#shipment_point").val(); //textbox value  
            if (shipment_point) {
                var dataString = 'shipment_title=' + shipment_title;
                var dataString = 'shipment_point=' + shipment_point;
                var values = $(this).serialize();
                ajaxRequest = $.ajax({
                    url: '<?php echo Router::url(array("controller" => "DispatchedJobs", "action" => "approxBudget")); ?>',
                    type: 'POST',
                    data: dataString,
                    dataType: "html",
                    beforeSend: function() {
                        $('.spinicon').show();
                    },
                    success: function(response) {
                        $("#vessels_table").html(response);
                    },
                    complete: function() {
                        $('.spinicon').hide();
                    }
                });
            }
        });
    });
</script>

//controller function
 public function approxBudget($shipment_point,$shipment_title)
    {
        echo $shipment_title;
        echo $shipment_point; exit();

    }

将参数作为data对象。 这就是您需要的一切。

$("#subvessels").on('click', function() {
    ajaxRequest = $.ajax({
        url: '<?php echo Router::url(array("controller" => "DispatchedJobs", "action" => "approxBudget")); ?>',
        type: 'POST',
        data: {
            shipment_title: $("#shipment_title").val(),
            shipment_point: $("#shipment_point").val()
        },
        dataType: "html",
        beforeSend: function() {
            $('.spinicon').show();
        },
        success: function(response) {
            $("#vessels_table").html(response);
        },
        complete: function() {
            $('.spinicon').hide();
        }
    });
});

在php中,默认情况下使用$_POST["shipment_title"]$_POST["shipment_point"]值。 如果使用框架,则可能会有其他方法来访问值。 但是请求像上面一样是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM