简体   繁体   中英

Passing Form ID variable to echo function within javascript

I am unable to pass the form id variable to javascript where the date input = mm/dd/yyyy with selection of dropdown text A and text B should populate the JS output

  $('#Option').change(function(){ if ($(this).val()== 'A') { $('#text1').val('3'); $('#text2').val("<?php echo date('m/d/Y', strtotime($myDate . ' +1 Weekdays')); ?>"); } else if ($(this).val()== 'B') { $('#text1').val('8'); $('#text2').val("<?php echo date('m/d/Y', strtotime($myDate . ' +8 Weekdays')); ?>"); } }); 
 <div class="col-xs-3"> <label class="control-label" placeholder="mm/dd/yyyy">Date</label> <input type="text" class="form-control" id="Date" name="Date" /> </div> <div class="col-xs-3"> <label class="control-label">Option</label> <select class="form-control" id="Option" name="Option"> <option value="">Select</option> <option value="A" >A</option> <option value="B" >B</option> </select> </div> <div class="col-xs-3"> <label class="control-label">Text1</label> <input type="text" class="form-control" id="Text1" name="Text1" /> </div> <div class="col-xs-3"> <label class="control-label">Text2</label> <input type="text" class="form-control" id="Text2" name="Text2" /> </div> 

https://jsfiddle.net/hufuso0d/3/

The ids are Text1 and Text2 . Try as below :

<script>
$('#Option1').change(function(){
    if ($(this).val()== 'A') {
        $('#Text1').val('3');
        $('#Text2').val("<?php echo date('m/d/Y', strtotime($myDate . ' +1 Weekdays')); ?>");
    } else if ($(this).val()== 'B') {
         $('#Text1').val('8');
         $('#Text2').val("<?php echo date('m/d/Y', strtotime($myDate . ' +8 Weekdays')); ?>");

        }

    });
 </script>

It works! Pay attention to the correct id: you should insert

$('#Text1').val('3');

instead of

$('#text1').val('3');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM