对于下面的形式
<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>
如何根据选择的是 A
还是 B
来更改输入字段的值?我认为它必须在 javascript 中完成。我在这里和那里使用过 jquery,所以可以根据它提出建议。非常感谢!
最佳答案
您不必使用 jQuery,(但 jQuery 确实让生活更轻松):
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
}
关于php - 基于所选选项值的表单输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7802235/