var isCheckboxChanged = false;
var checkboxInitialValue = $('#revert').is(":checked");
var checkboxValueAfterChange = null ;
$('#revert').on('change', function(){
isCheckboxChanged = true;
checkboxValueAfterChange = $('#revert').is(":checked");
$('#wasValueChanged').val(1);
if(checkboxValueAfterChange == null){
console.log("No Changes detected");
}
else{
console.log("Checkbox state is the same after change ?");
console.log(checkboxInitialValue == checkboxValueAfterChange);
}
$.ajax({
url: "do_action_on_checkbox_revert.php",
success: function(result){
$('.input_to_be_rendered_on_condition').removeClass('hidden');
$('.more_you_want_to_add_dynamically').html(result.information);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="revert">
<form>
<input type="hidden" id="wasValueChanged" value="0">
<input type="submit" name="submit" value="Submit"/>
</form>
<div class="hidden input_to_be_rendered_on_condition">
<p>Some stuff that is here</p>
<div class="more_you_want_to_add_dynamically"></div>
</div>
<!-- Later in PHP File (do_action_on_checkbox_revert.php) -->
<!--
$reverted = false;
if(isset($_POST['wasValueChanged']) && $_POST['wasValueChanged'] > 0){
$reverted=true;
}
$return_arr = array();
if($reverted){
$return_arr["information"] = "We like changing input boxes";
}
else{
$return_arr["information"] = "We hate changing input boxes";
}
echo json_encode($return_arr);
-->
<!-- You cannot use the PHP part if thee submit button is not clicked, when you submit the form this page should reload and the $reverted should be true
Вы можете использовать javascript для этого. Если вас интересует только то, щелкнул ли он когда-либо и был ли изменен, но не имеет значения, останется ли значение в конце неизменным или нет, вы можете использовать следующее:
var isCheckboxChanged = false;
$('#revert').on('change', function(){
isCheckboxChanged = true;
});
Если это имеет значение что значение отличается от того, с которого вы начали, вы можете сделать следующее:
var checkboxInitialValue = $('#revert').is(":checked");
var checkboxValueAfterChange = null ;
$('#revert').on('change', function(){
checkboxValueAfterChange = $('#revert').is(":checked");
if(checkboxValueAfterChange == null){
console.log("No Changes detected");
}
else{
console.log("Checkbox state is the same after change ?");
console.log(checkboxInitialValue == checkboxValueAfterChange);
}
});