2012-01-28 7 views
0

クリックするとAJAX呼び出しが実行されます。私はリスナーを設定していますが、何も起きません...火かき棒は何も表示しません。JQueryリスナーがリッスンしない

コード:あなたはイベントリスナーが設定されていない

<form method="post" action="profile/<?php echo $_SESSION['usern']; ?>/settings"> 
    <p><input type="checkbox" id="profile_visible" name="profile_visible" /> Show Profile<span id="resultProfileVisible"></span></p> 
    </form>  

おかげ

+2

どのリスナーを参照していますか?私はただ一つしか見ることができません - 文書は準備ができています。それはあなたが意味するものですか? –

答えて

1

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js{API removed}"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
alert('test'); 
if ($('#profile_visible:checked').val() !== null) { 

     $.ajax({ 
      url: 'inc/profileVisible.php', 
      success: function(data) { 
      $('#resultProfileVisible').innerhtml="success"; 
      alert('Load was performed.'); 
      } 
     }); 
} 
    } 
    </script> 

...と、文書の本文に。 on(jQueryの> 1.7)またはbind(jQueryの< 1.7)またはjQueryの各種ショートカット法(等.change又は.click)と結合イベントハンドラ:また

$(document).ready(function() { 
    $("#profile_visible").change(function() { 
     if (this.checked) { 
      $.ajax({ 
       url: 'inc/profileVisible.php', 
       success: function(data) { 
        $('#resultProfileVisible').html("success"); 
        alert('Load was performed.'); 
       } 
      }); 
     } 
    }); 
}); 

、場合エレメントのHTMLコンテンツを設定する.html()代わりに.innerHtmlを使用jQueryオブジェクトを使用します。

例:http://jsfiddle.net/andrewwhitaker/8x6h8/1/

+0

ご協力いただきありがとうございます! – mandalorianwarrior

1

唯一のリスナーは、DOM準備ができているので、一度だけ実行されます。

コードにはいくつか問題があります。下記参照。

$(document).ready(function() { 
    alert('test'); 
      // I assume you just want to see if it is checked 
    if ($('#profile_visible').is(':checked')) { 

     $.ajax({ 
      url: 'inc/profileVisible.php', 
      success: function (data) { 
        // jQuery has .html(), not innerhtml 
       $('#resultProfileVisible').html("success"); 
       alert('Load was performed.'); 
      } 
     }); 
    } 
}) // <-- was missing closing parentheses 
+0

ご協力いただきありがとうございます。大変感謝しています...私はjQueryの4年間の中断を経て、それに取り掛かろうとしています。自然にあなたの助言は助けました..今私は、ページの負荷に表示するための警告 "テスト"を取得します。しかし、チェックボックスをチェックすると、$ ajaxは起動しません。 PHPスクリプトへのパスは正しいです。 Firebugは "Script"の下で以下のことを教えてくれます:制限されたURIへのアクセスが拒否されました。ここでも、ajaxサーバー側のスクリプトへのパスは正しいです。助言?ありがとう – mandalorianwarrior

+0

@ user1175528:ディレクトリ構造は不明ですが、絶対パスが必要なのでしょうか? ''/ inc/profileVisible.php' '。クリックごとに実行する必要がある場合は、[Andrew Whitakerの答え](http://stackoverflow.com/a/9047445/1106925)を参照してください。 –

0

変更私はあなたのリスナーを見ていないよ、この

$(document).ready(function(){ 
$('#profile_visible').change(function(){ // handler when the checkbox is toggled 
    if($(this).attr('checked')) { // check if it is checked 
     $.ajax({ 
      url: 'inc/profileVisible.php', 
      success: function(data) { 
      $('#resultProfileVisible').html("success"); // change here 
      alert('Load was performed.'); 
      } 
     }); 
    } 
    }); 
}); 
+0

'if($(this).attr( 'checked')' => 'if(this.checked)' – gdoron

+0

ええ、あなたは正しい人です.. 1番目はjqueryで、もう1つは純粋なjavascriptです... –

+0

大変ありがとうございます、私はこのコードを実装しており、それはきれいに動作します。私は間違いを拝見しました..大変ありがとうございました – mandalorianwarrior

0

ようなコード。試してみてください

$(document).ready(function() { 
    $('#profile_visible').change(function() { 
     if ($(this).attr('checked')) { 
      $.ajax({ 
       url: 'inc/profileVisible.php', 
       success: function(data) { 
       $('#resultProfileVisible').html("success"); // change here 
        alert('Load was performed.'); 
       } 
      }); 
     } 
    }); 
}); 
+1

'if($(this).attr( 'checked')' => 'if(this。チェックしました) ' – gdoron

+0

ありがとうございました..私はあなたの投稿から学んだ。ありがとう – mandalorianwarrior

関連する問題