Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

php - Form input value based on option value selected

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...