Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从视图到cakePHP函数的jQuery调用_Javascript_Jquery_Cakephp_Cakephp 2.3 - Fatal编程技术网

Javascript 从视图到cakePHP函数的jQuery调用

Javascript 从视图到cakePHP函数的jQuery调用,javascript,jquery,cakephp,cakephp-2.3,Javascript,Jquery,Cakephp,Cakephp 2.3,在视图中,我有如下下拉列表: <div class="input select"><label for="customer">Customer</label><select name="data[Project][customer_id]" id="customer"> <option value="16">Customer1</option> <option value="17">Customer2</o

在视图中,我有如下下拉列表:

<div class="input select"><label for="customer">Customer</label><select name="data[Project][customer_id]" id="customer">
<option value="16">Customer1</option>
<option value="17">Customer2</option>
</select>
JQuery不是我擅长的东西,但我做了研究,并尝试使用一些类似的功能,使其工作,但没有任何东西简单地工作,我觉得我在做一些完全错误的事情。以下是jQuery:

    jQuery(document).ready(function($){

        $('#customer').change({
       var initials = "";
       $("select option:selected").each(function(){
            initials = "<?php echo Router::url(array('controller'=>'projects','action'=>'getInitialsById')) ?>"
});
$("div").text( initials );
        });

非常感谢您的帮助。

您需要正确设置AJAX调用:

此外,您还需要在AJAX调用中提供新选择的客户的值id,如getInitials$id函数声明中所定义的。请注意,您请求的是getInitials,而不是getInitialsById,因为前者调用后者


最后,我不相信echo会起作用。你需要做$this->response->bodyjson\u编码$initials

在文档加载时调用make-AJAX调用没有意义,因为这发生在从下拉列表中选择选项之前

因此,要在选择选项后进行AJAX调用,请添加以下代码:

$('#customer').change(function(){
    var data = $('#inputEl option:selected').val();

    $.ajax({
        type:'POST',
        async: true,
        cache: false,
        url: <?php echo Router::url(array('controller'=>'projects','action'=>'getInitialsById')) ?>,
        success: function(response) {
            jQuery('[ID OF ELEMENT TO HOLD REPONSE (OPTIONAL)').html(response);
        },
        data: data
    });
    return false;
})

还没有经过测试,但应该可以使用。

看起来您混合了一些客户端、jqueryeach循环和服务器端php。看看jQuery.getJson。另外,虽然我不熟悉cakephp,但您似乎在函数调用中缺少id参数,不是吗?
$('#customer').change(function(){
    var data = $('#inputEl option:selected').val();

    $.ajax({
        type:'POST',
        async: true,
        cache: false,
        url: <?php echo Router::url(array('controller'=>'projects','action'=>'getInitialsById')) ?>,
        success: function(response) {
            jQuery('[ID OF ELEMENT TO HOLD REPONSE (OPTIONAL)').html(response);
        },
        data: data
    });
    return false;
})