简体   繁体   中英

Clear a text input field when a DropDown Selection field is set to a certain value?

Based on the HTML Selection form field below, I need to reset a text field value when the selection field is set to any value besides Completed or Cancelled

<select name="status" id="status" title="">
    <option label="Open" value="Open">Open</option>
    <option label="In Progress" value="In_Progress">In Progress</option>
    <option label="Future_Project" value="Future_Project">Future_Project</option>
    <option label="Cancelled" value="Cancelled">Cancelled</option>
    <option label="Completed" value="Completed" selected="selected">Completed</option>
</select>

The Text input that I need to set the value to empty/nothing looks like this...

<input autocomplete="off" type="text" id="date_completed_date" value="09/20/2014 06:15pm" size="11" maxlength="10" onblur="combo_date_completed.update();" onchange="combo_date_completed.update(); ">

I can use jQuery or regular JavaScript. I would appreciate any help, I think this should be pretty simple?

Working solution: http://jsfiddle.net/bevanr01/7e2ubo85/4/

$( "#status" ).change(function() {
    if ((["Cancelled","Completed"].indexOf($(this).val()) == -1)){
         $('#date_completed_date').val('');   
    }
});

And if you wanted to get fancy as mentioned below you could add today's date those two values are selected.

$( "#status" ).change(function() {
    if ((["Cancelled","Completed"].indexOf($(this).val()) == -1)){
         $('#date_completed_date').val('');   
    } else {
            $('#date_completed_date').val(new Date());   
    }
});

Its worth retaining the Date if the user Re-selects another Option otherwise the date will stay Blank.

var date = $('#date_completed_date').val();
$("select").on("change", function () {

    if ($(this).val().indexOf('ed') == -1) {
   $('#date_completed_date').val('');
}
    else { 
        $('#date_completed_date').val(date); 
    }

})

Demo

http://jsfiddle.net/mrw9hor8/

try this

    <form name="sample">
    <select name="status" id="status" title="" onchange="clearInput()">
        <option label="Open" value="Open">Open</option>
        <option label="In Progress" value="In_Progress">In Progress</option>
        <option label="Future_Project" value="Future_Project">Future_Project</option>
        <option label="Cancelled" value="Cancelled">Cancelled</option>
        <option label="Completed" value="Completed" selected="selected">Completed</option>
    </select>
<input autocomplete="off" type="text" id="date_completed_date" value="09/20/2014 06:15pm" size="11" maxlength="10" onblur="combo_date_completed.update();" onchange="combo_date_completed.update(); ">
</form>

in your script

function clearInput() {
   if(document.sample.status.value != "Completed" && document.sample.status.value != "Cancelled")
   {
     document.getElementById('date_completed_date').value='';
   }

}

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