通过取消选中复选框将空值设置为文本字段


set empty value to a text field by deselecting checkbox

我有一个包含 2 个文本字段的表单,可以触发 jquery 日期选择器。

我想通过取消选中相同形式的复选框来为两个文本字段设置空值。

文本字段为:

<?php 
   if ($_GET["arrival"]== "") 
   echo ' 
   <label for="arrival">Arrival </label>
   <input type="text" name="arrival" class="w8em format-d-m-y highlight-days-67 range-low-today" id="arrival" value="Select Date"/> ';
   else
   echo ' 
   <label for="arrival">Arrival </label>
   <input type="text" name="arrival" class="w8em format-d-m-y highlight-days-67 range-low-today" id="arrival" value="' . $_GET["arrival"] . '"/> '; 
   ?>
<?php
   if ($_GET["departure"]== "") 
   echo ' 
   <label for="departure">Departure </label>
   <input type="text" name="departure" class="w8em format-d-m-y highlight-days-67 range-low-today" id="departure" value="Select Date"/> ';  
    else
    echo ' 
    <label for="departure">Departure </label>
    <input type="text" name="departure" class="w8em format-d-m-y highlight-days-67 range-low-today" id="departure" value="' . $_GET["departure"] . '"/> '; 
    ?>

复选框为:

<input name="avail" type="checkbox" id="avail"
                 <?php if ($_GET["avail"]== "1") echo "checked='"checked'""; ?>
                 value="1" />

我想通过取消选择它将空值设置为文本字段。

你可以使用 jquery 让它工作。

复选框 onclick event您可以为文本字段设置空值。

<script>
        function empty()
        {
           if($("#avail:checked").length > 0)
           {
              $('#arrival').val('');
              $('#departure').val('');
           }
        }
</script>

在复选框上添加单击事件

<input name="avail" type="checkbox" id="avail"
             <?php if ($_GET["avail"]== "1") echo "checked='"checked'""; ?>
             value="1" onclick="empty();"/>