2016-12-28 70 views
0

我无法获取JavaScript代码在cakephp3中工作。最初我想隐藏/显示一个div部分点击一个选项。除了这个js函数,该项目还可以正常工作。我有一个文件(webroot/js/myjs.js)中的js函数,我在下面的布局中调用该文件。我点击一个选项按钮来调用这个函数来测试它,我没有输出。我无法看到还有什么我需要做的,因为我卡住了。JavaScript上的选项按钮不会在cakephp3中工作

http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-inline-javascript-blocks 

<?php 

    $options=array(0=>'Student',1=>'Tutor'); 
    echo $this->Form->input('cancelledBy', 
      [ 'label'=>false, 'style'=>"margin:10px;", 'type' => 'radio', 'options' => $options, 
         'onclick'=>'hide()', 'value'=>0]); 

       ?> 

     <div id="cancel" style="display: none;">Hello hidden content</div> 


//myjs.js 
    <script type="text/javascript"> 
function hide() 
{ 
alert('asd'); 



    var e = document.getElementById("cancel"); 

     if(e.style.display == 'block') 
      e.style.display = 'none'; 
     else 
      e.style.display = 'block'; 



} 
</script> 


//layoutfile (which appears when i see the page source with the javascript function 
<?php echo $this->Html->script(['myjs.js']); ?> 
+0

你的代码看起来都good..is该警报的表现?控制台是否显示一些错误? –

+0

我试了答案建议,这没有奏效,也没有警报不会触发。我真的不知道该怎么做,所以我有一个公共副本http://crm5.aptutoring.com.au/lessons/cancelledlessontest – jagguy

回答

1

使用这个在你看来$this->Html->script('myjs', ['block' => 'scriptBottom']); 之前</body>

编辑此<?= $this->fetch('scriptBottom') ?>添加到您的布局

删除'type' => 'radio'和改变的onclick事件的onchange

$options = array(0=>'Student',1=>'Tutor'); 

echo $this->Form->input('cancelledBy', [ 
    'label' => false, 
    'style' => "margin:10px;", 
    'options' => $options, 
    'onchange' => 'hide()', 
]); 
+0

这也没有工作。我必须缺少一些东西http://crm5.aptutoring.com.au/lessons/cancelledlessontest – jagguy

+0

显然onclick不能用于收音机布景,请检查我的编辑。 – SNA

0

这工作

//鉴于

echo $this->Form->input('cancelledBy', [ 
    'class' => 'cancelledByClass', 
    'label' => false, 
    'style' => "margin:10px;", 
    'type' => 'radio', 
    'options' => $options, 
    'value' => 0 
]); 

// JS

$('input[name="cancelledBy"]').change(function(){ 
    alert('Here'); 
    var e = document.getElementById("cancel"); 

    if (e.style.display == 'block') 
    e.style.display = 'none'; 
    else 
    e.style.display = 'block'; 
});