2016-07-14 17 views
-2

少なくとも1つのチェックボックスがオンになっていることを確認するために、次のコードを使用しています。チェックボックスの値を「cont」というIDに出力します。私は代わりに配列(PHP)に出力を取得する方法を探しています。私はJSをあまりよくしていません。それでもトラブルPHP変数にチェックを入れ、チェックボックスから値を取得したPHPのチェックボックスの値

<?php 
$count = '0'; 
while($count <= '2') { 
    $count++; 
    $the_name = "chkBx"; 
    $the_value = 'checkbox'.$count; 
    if ($count == '1') { 
     $its_checked = 'checked'; 
    } else { 
     $its_checked = ''; 
    } 
    ?> 
    <input type="checkbox" name="<?php echo $the_name; ?>" value="<?php echo $the_value; ?>" <?php echo $its_checked; ?> > 
    <?php 
} 
?> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<div id="cont"></div><!-- OUTPUTS HERE--> 

<script> 
$('input[type="checkbox"][name="<?php echo $the_name; ?>"]').on('change',function(){ 
    var getArrVal = $('input[type="checkbox"][name="<?php echo $the_name; ?>"]:checked').map(function(){ 
    return this.value; 
    }).toArray(); 

    if(getArrVal.length){ 
    //execute the code 
    $('#cont').html(getArrVal.toString()); 

    } else { 
    $(this).prop("checked",true); 
    $('#cont').html("At least one value must be checked!"); 
    return false; 

    }; 
}); 
</script> 

アップデートは、ここで私が今持っているものです:あなたはの配列を

<?php 
$the_name = "myCheck"; 
?> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
1.<input type="checkbox" class="<?php echo $the_name; ?>" name="the_name" value="1" checked> 
2.<input type="checkbox" class="<?php echo $the_name; ?>" name="the_name" value="2"> 
3.<input type="checkbox" class="<?php echo $the_name; ?>" name="the_name" value="3"><br/> 
<span class="cont" name="bob" value="9999"></span> 

<script> 
function checkIt() { 
    var getArrVal = $(".myCheck:checked").map(function() { 
     return this.value; 
    }).get(); 
    if (getArrVal.length) { 
     $('.cont').html(getArrVal.join(",")); 

     var x=$(".cont").text(); 
     var variablename = $(".cont").val(); 

    } else { 
     $('.cont').html("At least one value must be checked!"); 
     $(this).prop("checked", true); 
     getArrVal=[$(this).val()]; 
     return false; 
    } 
    console.log(getArrVal); 
} 
$(function() { 
    $(".myCheck").on('click',checkIt); 
    checkIt(); 
}); 
</script> 
+0

何が配列を必要としますか?またマップは配列を返します – mplungjan

+0

あなたはAJAXを使ってPHPに出力したいかもしれませんが、あなたの質問を理解していないかもしれません – Ahmad

答えて

0

何が必要ですか?

そうした場合、あなたは負荷にし、チェックボックスは、私が個人的に短いスクリプトを作るchekcbox上のクラスを使用します

を取り外すことができないの両方、それを設定しておく必要があります。

function checkIt() { 
 
    var getArrVal = $(".myCheck:checked").map(function() { 
 
     return this.value; 
 
    }).get(); 
 
    if (getArrVal.length) { 
 
     $('#cont').html(getArrVal.join(",")); 
 
    } else { 
 
     $('#cont').html("At least one value must be checked!"); 
 
     $(this).prop("checked", true); 
 
     getArrVal=[$(this).val()]; 
 
     return false; 
 
    } 
 
    console.log(getArrVal); 
 
} 
 
$(function() { 
 
    $(".myCheck").on('click',checkIt); 
 
    checkIt(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
1.<input type="checkbox" class="myCheck" name="the_name" value="1" checked> 2.<input type="checkbox" class="myCheck" name="the_name" value="2"> 
 
3.<input type="checkbox" class="myCheck" name="the_name" value="3"><br/> 
 
<span id="cont"></span>

+0

このチェックボックスはループ内にありますので、値を配列に入れて後で。 これはループで動作しますか? –

+0

私のコードは、入力をどのように生成するか気にしません。サーバ上で配列を受け取るには、通常PHPを使用するとき 'name =" somename [] "'を与えます – mplungjan

+0

私は最初にname = "somename []"を使用しようとしましたが、JSはループの最後の値しか保存しませんでした。 –

関連する問題