简体   繁体   English

我需要显示已存储在数据库中的日期/日期时间字段,基于<select>标记值

[英]I need to display Date/Datetime field already stored on Database, based upon the <select> tag value

I'm not good in English sorry for that.我英语不好,抱歉。 I try to convey my problem as much as i can.我尽量表达我的问题。

I created edit page in PHP to edit already saved data.我在 PHP 中创建了编辑页面来编辑已保存的数据。 The edit page will display already saved data's near each record there will be edit button after clicking the button model window will open to edit the saved data's., Right on that i facing some challenge.编辑页面将在每条记录附近显示已保存的数据,单击按钮模型窗口将打开以编辑保存的数据后将出现编辑按钮。,就在这我面临一些挑战。

I need to display Date/Datetime field already stored on Database, based upon the course type already selected.我需要根据已选择的课程类型显示已存储在数据库中的日期/日期时间字段。 (Eg: If the course type value was already selected as ==1 then automatically date field with value should display, If the value selected as ==2 then datetime field with value should display) Hereby i attached the code., (例如:如果课程类型值已经选择为 ==1,则应自动显示带值的日期字段,如果选择为 ==2 的值,则应显示带值的日期时间字段)特此附上代码。,

<select name="selectCourseType" id="editCourseType" required>
    <option value=""  required>Select Courses Type</option>
<?php 
    $sqlEdit = mysqli_query($link,"SELECT * FROM courses_type ORDER BY ct_id DESC");
    $row = mysqli_num_rows($sqlEdit);
    while ($row = mysqli_fetch_array($sqlEdit)){
        $ctID = $row['ct_id'];
        $ctName = $row['ct_name'];
?>
    <option <?php if ($ctID==$ctType){ ?> selected="selected" <?php } ?>  value="<?php echo $ctID ?>" ><?php echo $ctName ?></option>
        <?php } ?>
</select>
                    
                
<div id="editDeadlineDtfrom" hidden>                           
    <input class="form-control" type="date" name="deadlinedatefrom" />                        
</div>

<div id="editDeadlineDtto" hidden>                           
     <input class="form-control" type="date" name="deadlinedateto" />                          
</div>

<div id="editDurationDtfrom" hidden>                          
    <input class="form-control" type="datetime-local" name="durationdatefrom" />                         
</div>

<div id="editDurationDtto" hidden>                  
    <input class="form-control" type="datetime-local" name="durationdateto" />                            
</div>


 <script>
    document.getElementById("editCourseType").addEventListener("change", function() {
        const isDeadline = this.value === "2"
        document.getElementById("editDeadlineDtfrom").hidden = !isDeadline
        document.getElementById("editDeadlineDtto").hidden = !isDeadline
    })
    document.getElementById("editCourseType").addEventListener("change", function() {
        const isDuration = this.value === "1"
        document.getElementById("editDurationDtfrom").hidden = !isDuration
        document.getElementById("editDurationDtto").hidden = !isDuration
    })

</script>

You can use a single event handler to process the show/hide of the date,datetime elements.您可以使用单个事件处理程序来处理日期、日期时间元素的显示/隐藏。 With a few minor alterations to the HTML ( added new classNames to certain elements to aid selection/identification and streamline code ) you could do something like this.通过对 HTML 进行一些小的改动(向某些元素添加新的类名以帮助选择/识别和简化代码),您可以执行类似的操作。

Use a dedicated class to hide the elements and remove that class based upon the selection made by the user.使用专用类隐藏元素并根据用户所做的选择删除该类。

 document.querySelector('select[name="selectCourseType"]').addEventListener('change', function(e) { const cn = 'hidden'; //re-hide all DIV elements document.querySelectorAll('.ctrls').forEach(n => { if( !n.classList.contains(cn) ) n.classList.add(cn); }); // unhide specific DIV elements based upon user selection switch (Number(this.value)) { case 1: document.querySelectorAll('.duration').forEach(n => n.classList.remove(cn)); break; case 2: document.querySelectorAll('.deadline').forEach(n => n.classList.remove(cn)); break; } });
 .hidden { display: none; } .ctrls{ margin:0.25rem; padding:0.25rem; }
 <select name="selectCourseType" required> <option selected hidden disabled>Select Courses Type <option value=1>1-Duration <option value=2>2-Deadline </select> <div class='ctrls hidden deadline'> <input class="form-control" type="date" name="deadlinedatefrom" /> </div> <div class='ctrls hidden deadline'> <input class="form-control" type="date" name="deadlinedateto" /> </div> <div class='ctrls hidden duration'> <input class="form-control" type="datetime-local" name="durationdatefrom" /> </div> <div class='ctrls hidden duration'> <input class="form-control" type="datetime-local" name="durationdateto" /> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在gridview中选择日期时间,并在gridview中选择日期后更改下一个文本框值 - select datetime in gridview and change next textbox value upon date selection in gridview 如何根据输入字段的值在span标记中显示文本 - How to display text in a span tag based on the value of input field 基于选择的角度过滤器特定字段 - Angular filter specific field based upon select Ruby on Rails:select_tag如何根据所选值显示 - Ruby on rails: select_tag how to display based on selected value 如何从数据库显示值到 tokenize2 select 字段? - how to display value from database to tokenize2 select field? 单击超链接文本后,如何将锚标记中存在的值设置为隐藏字段(即锚标记中存在的值)? - How to set a value present in anchor tag to a hidden field upon clicking on the hyperlink text(i.e. value present in anchor tag)? 我如何从数据库中回显值 INTO html<select>标签? - How can i echo value from database INTO html <select> tag? 如何在不提交 im html 的情况下根据已选择的值显示新的 select 值 - How to display new select values based on already selected value without submitting im html 如何使用数据库中的 DateTime 值在反应日历上显示日期 - How to display date on react-calendar using DateTime value from database 如何根据当前所选按钮的上下文选择正确的子元素隐藏的输入值? - How can I select the correct child element hidden input value based upon the context of the current selected button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM