2016-09-24 8 views
-1

以下のスクリプトが.jsファイルを使ってインクルードされていますが、起動されません。私はJQueryの新機能で、どこが間違っているのか分かりません。checkbox onChangeは発動しません

お願いします。私は今、次のように変更して以下にスクリプトを変更した

: は私を修正するテオフィロスのサンプルコードを使用し、それが正しく発射おかげ ジュリアン$(この).ATTR(「:チェックするには、」)は動作しません。それは "unasigned"を返すように私のために。しかし、.propを使用すると、$(this).prop( 'checked')が期待どおりに動作します(true/false)。 Rashet、テスト目的のために、URLを '/my.script.php'に変更しました。まだ動作しません。

if(isset($_POST['id']) || isset($_POST['Purchased'])){ 
die('Eureka! variables exist'); 
}else{ 
die('Failure!, variables do not exist'); 
} 

$('input[name=Purchased]').change(function(){ 
    //if a checkbox with name 'Purchased' is clicked, do the following. 
    var id=$(this).attr('id'); //grab the id from the clicked box 
    var Purchased=$(this).prop('checked'); 

    //alert("ID value:"+id); returns correctly 
// alert("Purchase value:"+Purchased); //Returns correctly 

    //setup the ajax call 
//Not posting the variable values to PHP processing script 
    $.ajax({ 
     type: 'POST', 
     url: '/my.script.php', 
     data: {id: 'id', 
     Purchased: 'Purchased'} 
    }); 
}); 
+0

URLが間違っている中で、それを見るためにここにそれについてまで読むことができるはずです。ローカルファイルシステムを参照することはできず、現在の場所の上にあるものは参照できません。 –

+0

コードにphpとjavascriptの両方が含まれている場合は、phpタグを追加してください。 –

答えて

1

これは.is(selector)この記事https://stackoverflow.com/a/12279447/5836034からの助けと、あなたは以下のJquery doc on is() 実行コードスニペットは、アクション

$('input[name=Purchased]').change(function(){ 
 
    
 
    if (!$(this).is(":checked")) { 
 
     // not checked 
 
    alert("Not Checked"); 
 
     return; 
 
    } 
 
    
 
    alert("Checked continue"); 
 
    //check here 
 
    
 
    //if a checkbox with name 'Purchased' is clicked, do the following. 
 
    var id=$(this).attr('id'); //grab the id from the clicked box 
 
    var Purchased=$(this).val(); //grab the value from the checkbox (remember, if it is checked it will be 'on', else '' 
 

 
    alert("Purchase value:"+Purchased); 
 

 
    //setup the ajax call 
 
    $.ajax({ 
 
     type: 'POST', 
 
     url: '../../Includes/MyPHP.script.php', 
 
     data: {id: 'id', 
 
     Purchased: 'Purchased'} 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<input type="checkbox" value="value here" name="Purchased" >

+0

テオフィラスのおかげで、サンプルコードがはっきりと分かりました。 –

+0

Owkは、助けてくれたらあなたの答えとしてマークすることができます。ありがとうございました –

0

$(this).val()$(this).prop('checked')すべきであり、それはtrueまたはfalseを返します。

+0

ジム私は$(this).attr( ':checked')は動作しませんでしたが、$(this).prop( 'checked')は動作します。ご協力いただきありがとうございます。 –

+0

あなたは正しいです、私の間違い。編集された答え。 –

関連する問題