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

php - Form input value based on option value selected - Stack Overflow

programmeradmin1浏览0评论

for the following form

<form action ='search.php' method = 'post'>

<select name="filter">
<option id="A" value="A">A</option>
<option id="B" value="B">B</option>
</select>

<input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />

<input type ='submit' value = 'Search' />

</form>

How do i make the value of the input field change based on whether A or B is selected? I assume it has to be done in javascript. I have used jquery here and there so it is ok to make suggestions based on it. Thanks alot!

for the following form

<form action ='search.php' method = 'post'>

<select name="filter">
<option id="A" value="A">A</option>
<option id="B" value="B">B</option>
</select>

<input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />

<input type ='submit' value = 'Search' />

</form>

How do i make the value of the input field change based on whether A or B is selected? I assume it has to be done in javascript. I have used jquery here and there so it is ok to make suggestions based on it. Thanks alot!

Share Improve this question asked Oct 18, 2011 at 3:53 algorithmicCoderalgorithmicCoder 6,78921 gold badges75 silver badges118 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 9

You don't have to use jQuery, (but jQuery does makes life easier):

HTML:

<form action ='search.php' method = 'post'>
    <select id="filter" name="filter">
    <option id="A" value="A">A</option>
    <option id="B" value="B">B</option>
    </select>

    <input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />

    <input type ='submit' value = 'Search' />    
</form>

Javascript:

document.getElementById('filter').onchange = function () {
    document.getElementById('field').value = event.target.value  
}

Example

you may try this

<script type="text/javascript">
function changeValue(){
    var option=document.getElementById('filter').value;

    if(option=="A"){
            document.getElementById('field').value="A Selected";
    }
        else if(option=="B"){
            document.getElementById('field').value="B Selected";
        }

}
</script>
<form action ='search.php' method = 'post'>

    <select name="filter" id="filter" onchange="changeValue();">
<option id="A" value="A">A</option>
<option id="B" value="B">B</option>
</select>

<input type ='text' name="key" id ="field" value ="something that changes based on what option is selected" />

<input type ='submit' value = 'Search' />

</form>

give id filter to select

 $(document).ready(function(){  
   var text_val;  
   $('#filter').change(function(){  
     text_val = $(this).val();
     $('#field').attr('value',text_val);  
     return false;  

   });

});

Try....

$('select[name=filter]').change(function() {
  var input_field = $('input#field');
  switch ($(this).val()) {
    case 'A': input_field.val('Value if A is chosen'); break;
    case 'B': input_field.val('Value if B is chosen'); break;
  }
});
发布评论

评论列表(0)

  1. 暂无评论