简体   繁体   English

CakePHP问题:如何从单选按钮中获取价值以及如何调用动作?

[英]CakePHP question: How can i get value from radiobutton and how can i call an action?

<div id="rightsidesix" >

    <?php echo '<div class="class_color_title" ><b>'."Polls".'</b></div>'; ?>

    <?php

        echo '<div id="id_color_polls" >';

            echo '<p><b>Which linux distribution do you like most ?</b></p>'.'<br/>';

            $options=array('F'=>'Fedora','U'=>'Ubuntu','D'=>'Debian','O'=>'openSuse','M'=>'Mandriva','T'=>'Other');

            $attributes=array('legend'=>false);

            echo $this->Form->radio('distributions',$options,$attributes);

            echo $this->Form->button('Check',array('id'=>'id_btnpolls'));

        echo '</div>';

        echo '<br/>';

    ?>

</div>      

Should i add these form input/radio buttons in a form ? 我应该在表单中添加这些表单输入/单选按钮吗?

After clicking the button it will call jQuery function. 单击按钮后,它将调用jQuery函数。

$("#id_btnpolls").click(function(){



    var valCheckedRadio = $('input[name=data[distributions]]:checked').val()

    alert(valCheckedRadio);



    $.ajax({

        type: "POST",

        url: "<?php Router::url(array('controller'=>'posts','action'=>'polls')); ?>,",

        data: "valCheckedRadio="+valCheckedRadio,

        success: function(prm){

            //alert(prm);

            $("#id_color_polls").html(prm);

        }

    });
})

Is this correct way to call an action of a controller ? 这是调用控制器动作的正确方法吗?

        url: "<?php Router::url(array('controller'=>'posts','action'=>'polls')); ?>,",
$("#id_btnpolls").click(function(){



var valCheckedRadio = $('input[name=data[distributions]]:checked', '#myForm').val()

alert(valCheckedRadio);



$.ajax({

    type: "POST",

    url: "<?php $html->url(array('controller'=>'posts','action'=>'polls')); ?>,",

    data: "valCheckedRadio="+valCheckedRadio,

    success: function(prm){

        //alert(prm);

        $("#id_color_polls").html(prm);

    }

  });
})

Whatever you've done looks absolutely fine for me. 无论您做什么,对我来说都算不错。 You can use something like this to get a value from radio group. 您可以使用类似的方法从广播组获取值。

$('input[name=radioName]:checked', '#myForm').val()

As per your url part its perfectly fine with cakephp. 根据您的网址部分,它与cakephp完全一样。

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

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