最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Clear value from input field when select value from dropdown list - Stack Overflow

programmeradmin0浏览0评论

I have this drop down list:

<select name="group_psoft" id="group_psoft">
    <option value="0" selected="selected" title="0">---select---</option>
    <option value="100" title="100">100</option>
    <option value="200" title="200">200</option>
    <option value="300" title="300">300</option>
</select>

and one input field:

<input id="name" value="">

How do I clear out the value of the "name" input when I change value in the dropdown list?

Kind regards

I have this drop down list:

<select name="group_psoft" id="group_psoft">
    <option value="0" selected="selected" title="0">---select---</option>
    <option value="100" title="100">100</option>
    <option value="200" title="200">200</option>
    <option value="300" title="300">300</option>
</select>

and one input field:

<input id="name" value="">

How do I clear out the value of the "name" input when I change value in the dropdown list?

Kind regards

Share Improve this question edited Nov 9, 2017 at 15:15 talemyn 7,9804 gold badges33 silver badges52 bronze badges asked Nov 9, 2017 at 15:05 PSoftPSoft 951 gold badge2 silver badges10 bronze badges 2
  • It's been a while since I used jQuery, you should be able to add an onChange handler to the select element. Then whenever it gets select, $('#name').val() to change the input value – Robbie Milejczak Commented Nov 9, 2017 at 15:06
  • $('#group_psoft').change(function() {$('#name').val('')}); – Daniel Beck Commented Nov 9, 2017 at 15:07
Add a ment  | 

3 Answers 3

Reset to default 3

Simply use change() function in jquery which triggers when you change the select like this

Read Here about .change()

$('#group_psoft').change(function() {$('#name').val('')}); 
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="group_psoft" id="group_psoft">
<option value="0" selected="selected"title="0">---select---</option>
<option value="100" title="100">100</option>
<option value="200" title="200">200</option>
<option value="300" title="300">300</option>
</select>
<input id="name" value="Initial Value">

You just need to use the change event of the list and then update the input on each occurrence of change.

$('#group_psoft').change(function() {
  // Set the input's value to the current value of the list
  $('#name').val(this.value);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="group_psoft" id="group_psoft">
<option value="0" selected="selected"title="0">---select---</option>
<option value="100" title="100">100</option>
<option value="200" title="200">200</option>
<option value="300" title="300">300</option>
</select>
<input id="name" value="Initial Value">

This will clear the input field when the dropdown selection changes:

$("#group_psoft").on("change", function(){
  $("#name").val("");
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="group_psoft" id="group_psoft">
<option value="0" selected="selected"title="0">---select---</option>
<option value="100" title="100">100</option>
<option value="200" title="200">200</option>
<option value="300" title="300">300</option>
</select>

<input id="name" value="">

发布评论

评论列表(0)

  1. 暂无评论