admin管理员组

文章数量:1331634

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 autoplete="off" type="text" id="date_pleted_date" value="09/20/2014 06:15pm" size="11" maxlength="10" onblur="bo_date_pleted.update();" onchange="bo_date_pleted.update(); ">

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

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 autoplete="off" type="text" id="date_pleted_date" value="09/20/2014 06:15pm" size="11" maxlength="10" onblur="bo_date_pleted.update();" onchange="bo_date_pleted.update(); ">

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

Share Improve this question asked Sep 21, 2014 at 3:21 JasonDavisJasonDavis 49k107 gold badges326 silver badges558 bronze badges 1
  • @PM77-1 perhaps it being too late for me to think as clearly as I generally do, I don't have a good or valid excuse but I got the help I requested. – JasonDavis Commented Sep 21, 2014 at 4:05
Add a ment  | 

3 Answers 3

Reset to default 5

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

$( "#status" ).change(function() {
    if ((["Cancelled","Completed"].indexOf($(this).val()) == -1)){
         $('#date_pleted_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_pleted_date').val('');   
    } else {
            $('#date_pleted_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_pleted_date').val();
$("select").on("change", function () {

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

})

Demo

http://jsfiddle/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 autoplete="off" type="text" id="date_pleted_date" value="09/20/2014 06:15pm" size="11" maxlength="10" onblur="bo_date_pleted.update();" onchange="bo_date_pleted.update(); ">
</form>

in your script

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

}

本文标签: javascriptClear a text input field when a DropDown Selection field is set to a certain valueStack Overflow