JQuery method call under body load

In the php application, add new data and change the existing data on one page, there are switches for choosing yes or no, but it works fine, causing

$(document).ready(function(){

    $('input[type="radio"]').click(function(){
        if($(this).attr("value")=="yes"){
            $(".box").hide();
            $(".buttn").show();
        }
        if($(this).attr("value")=="no"){
            $(".box").hide();
            $(".buttn").show();
        }
     });
  });

});

above, but for modification on page loading, I want to pass the value to the above method based on the value of the database table. as on the body tag calling a specific JS method.

+4
source share
2 answers

If I understood you well, you just need to change your code to be some kind of user-defined function, not jquery on loading the body. Try it:

$.fn.myfunc = function(){

    $('input[type="radio"]').click(function(){
        if($(this).attr("value")=="yes"){
            $(".box").hide();
            $(".buttn").show();
        }
        if($(this).attr("value")=="no"){
            $(".box").hide();
            $(".buttn").show();
        }
     });
  });

};

and when you pull the content from the database, after that you just call it with:

$. Fn.myfunc ()

,

$. fn.myfunc(var1, var2),

HTH

0

HTML-. , . PHP :

<?php
$boxClass = 'box';
$btnClass = 'buttn';
if ($valueFromDb == 'yes') {
  $boxClass .= ' hidden';
} else {
  $btnShow .= ' hidden';
}
?>
<!-- some HTML code -->
<div class="<?= $boxClass; ?>">Here is a box</div>
<div class="<?= $btnClass; ?>">Here is a button</div>

.hidden CSS .hidden { display: none; }. , JS.

UPD: , if HTML- :

<?php if ($someValue == true) { ?>

<!-- render first HTML block -->

<?php } else { ?>

<!-- render another HTML block -->

<?php } ?>

.

0

All Articles