Saya seorang pemula dan saya memiliki proyek ini di mana pengguna harus memiliki opsi input khusus jika opsi yang tercantum tidak ada di dropdown.

HTML

<div class="form-group">
            <label  class="col-sm-2 col-form-label">Select Page Size</label>
            <select name = 'pageSelector' class="col-sm-3">   
                <option value ="">Select Page Size</option> 
                <option value ="84.1|118.9">A0</option> 
                <option value = "59.4|84.1">A1</option> 
                <option value = "7.4|10.5">A7</option> 
                <option value = "custom">Select Custom</option> 
            </select> 
        </div> 

PHP

if(isset($_POST["pageSelector"]))
{
    $result = $_POST['pageSelector'];
    if($result == "")
    {
        echo "<script>alert('Please select the Page')</script>";   
    }
    
    $result_explode = explode('|', $result);
    $width_page = $result_explode[0];
    $height_page = $result_explode[1];
    

    // Converting the string variables to integer
    $width_plate=(double)$width_plate;
    $height_plate=(double)$height_plate;
    $width_page=(double)$width_page;
    $height_page=(double)$height_page;


    // To calculate the number of pages that can be print with one selected plate
    $calculated_width = $width_plate/$width_page;
    $calculated_height = $height_plate/$height_page;
    $print_include = (int)$calculated_height*(int)$calculated_width;

    echo "<div class='h1'>Number of Prints in one plate ".$print_include." prints</div> ";

}

Saya ingin jika pengguna memilih opsi khusus maka teks input akan muncul di layar.

0
Sibghat 9 Januari 2021, 15:53

3 jawaban

Jawaban Terbaik
var pageSelector = document.getElementById('pageSelector');
var customInput = document.getElementById('customInput');

pageSelector.addEventListener('change', function(){
    if(this.value == "custom") {
        customInput.classList.remove('hide');
    } else {
        customInput.classList.add('hide');
    }
})
.hide {
    width: 0;
    height: 0;
    opacity: 0;
}
<div class="form-group">
    <label  class="col-sm-2 col-form-label">Select Page Size</label>
    <select name = 'pageSelector' class="col-sm-3 page" id="pageSelector">
        <option value ="">Select Page Size</option> 
        <option value ="84.1|118.9">A0</option> 
        <option value = "59.4|84.1">A1</option> 
        <option value = "7.4|10.5">A7</option> 
        <option value = "custom">Select Custom</option>
    </select>
    <input type="text" class="hide" placeholder="Custom Selector" name="custom" id="customInput">
</div>
1
Hasan 9 Januari 2021, 13:24

Jika pengguna memilih opsi khusus maka Anda dapat memberinya masukan.

let selectEl = document.getElementById('select-list');

selectEl.addEventListener('change', (e) => {
  if (e.target.value == 'custom') {
    document.getElementById('txt-custom').style.display = 'block';
  } else {
    document.getElementById('txt-custom').style.display = 'none';
  }
});
#txt-custom {
  display: none;
}
<select id="select-list">
  <option value="">Select an option</option>
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="custom">Custom</option>
</select>
<input type="text" id="txt-custom" name="custom-value" />
2
Kostas 9 Januari 2021, 13:05

Kode Demo :

Pertama, Anda harus memiliki input dengan style="display:none" dan dengan jQuery

jQuery(document).ready(function() {
    jQuery("#selectId").change(function() {
        if (jQuery(this).val() === 'custom'){ 
            jQuery('input[name=other_input]').show();   
        } else {
            jQuery('input[name=other_input]').hide(); 
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<select name = 'pageSelector' class="col-sm-3" id="selectId" >   
  <option value ="">Select Page Size</option> 
  <option value ="84.1|118.9">A0</option> 
  <option value = "59.4|84.1">A1</option> 
  <option value = "7.4|10.5">A7</option> 
  <option value = "custom">Select Custom</option> 
</select> 
<br><br><br>
<input type="text" name="other_input" style="display:none" />
0
KUMAR 9 Januari 2021, 13:43