Wednesday, April 13, 2011

jquery: selectbox with diffrent output on diffrent selections

Hello! I'm trying to get input box to show different dates depending on the users selection from select box. If user select today today date should show if tomorrow, the date for tomorrow.

My qustion is how do I get the select box to show the correct date?

<?php 
$tomorrow  = date("Y-m-d", mktime(0, 0, 0, date("m")  , date("d")+2, date("Y")));
$today = date("Y-m-d", mktime(0, 0, 0, date("m")  , date("d"), date("Y")));
?>

<script>
$(document).ready(function(){

$("#select-bil").change(function () {
<?php            
echo      'var datum = "'.$tomorrow.'";'
?>    
     var str ="";
     $("#select-bil option:selected").each(function () {
       });
      $("input[name$='div-one']").val(datum);
    })
    .change();
});
</script>
From stackoverflow
  • I guess, it’s as easy as

    $('#select-option-for-today').val ('<?= $today ?>')
    $('#select-option-for-tomororrow').val ('<?= $tomorrow ?>')
    

    (If <?= doesn’t work in your config, try to <?php echo the same)

  • not entirely sure what you're trying to do, try this though

    <?php
    $tomorrow  = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d")+1, date("Y")));
    $today = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
    ?>
    
    <script>
    $(document).ready(function(){
        <?php
         echo 'var tomorrow = "'.$tomorrow.'";';
         echo 'var today = "'.$today.'";';
        ?>
        $("#select-bil>option:contains('today')").val(today);
        $("#select-bil>option:contains('tomorrow')").val(tomorrow);
    
        $("#select-bil").change(function () {
         $("input[name$='div-one']").val($("#select-bil option:selected").val());
        }).change();
    });
    </script>
    <input name="div-one" />
    <select id="select-bil">
     <option value="today">today</option>
     <option value="tomorrow">tomorrow</option>
     <option value="some other day">some other day</option>
    </select>
    

0 comments:

Post a Comment