2014-04-29 40 views
0

我想在jQuery中編寫一個函數,當用戶點擊一個複選框時,textfield-2變爲enabeled並且需要並且textfield-1被禁用。如果未選中複選框,則textfield-2將被禁用且不是必需的,並且textfield-1將被啓用。使用jQuery啓用/禁用textfield通過點擊複選框黑洞

所以就像這兩個文本框之間的切換。表格是某事像這樣:

<form id="link" name="link" action="post"> 
    <input type="checkbox" name="url_type" id="url_type" > 
    <input type="text" name="textfield1" id="textfield1"> 
    <input type="text" name="textfield2" id="textfield2" disable="disable"> 
    <input type="submit" name="send" value="send"> 
    </form> 

而jQuery代碼看起來是這樣的:

<script type="text/javascript"> 
    $('#url_type').on('change',function(){ 
     var checked=$('#url_type').is(':checked'); 
     if(checked==true) 
     { 
      $('#textfield1').attr('disabled',true); 
      $('#textfield2').attr('disabled',false); 
     } else { 

      $('#textfield1').attr('disabled',false); 
      $('#textfield2').attr('disabled',true); 
     } 
     }); 
</script> 

我的問題是,在CakePHP中,當我創建一個新的連接,如果沒有選中複選框工作正常,但如果我檢查複選框(意味着textfield2變爲啓用並禁用textfield1),它將生成如下所示的錯誤:

請求已被黑洞錯誤:請求的地址未找到這臺服務器。

我不知道衝突在哪裏,或者是什麼導致了這個錯誤!

+0

它可能與['SecurityComponent'](http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#security)有關。檢查['csrf'](http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#csrf-configuration)部分 – noslone

回答

1

您的網址可以處理Blackhole issue

將以下行放在控制器類文件的beforeFilter method中。

$this->Security->blackHoleCallback = 'blackhole'; 

現在,在同一個控制器文件中創建一個函數:

public function blackhole($type) { 
// handle errors. 
} 

This post也可以幫你解決這個問題。

0

適應你的表單標籤: 1.使用類型:後 2.類型屬性的動作,你的表格應發(行動/控制器的名稱/姓名)

<form id="link" name="link" method="post" action="/controller/action"> 
    <input type="checkbox" name="url_type" id="url_type" > 
    <input type="text" name="textfield1" id="textfield1"> 
    <input type="text" name="textfield2" id="textfield2"> 
    <input type="submit" name="send" value="send"> 
    </form> 
+0

不是表單的錯誤重定向,因爲如果我不禁用textfield2上的屬性,它工作完美... – landi

+0

但你提到了錯誤:在這臺服務器上找不到請求的地址。 – Simon

+0

我想,還有進一步的依賴關係,沒有顯示在您的發佈代碼上。 – Simon