简体   繁体   English

如何从下拉菜单到文本字段获取内容?

[英]How to get content from a drop down menu to text field?

I am a new person in this PHP and Javascript. 我是这个PHP和Javascript的新手。 I have a drop down menu as follows. 我有一个下拉菜单,如下所示。 Want to get the content or value to a text field and retain the value after the page refreshs? 是否希望将内容或值获取到文本字段并在页面刷新后保留值? How will do this? 怎么做?

<select name="animal" style="width: 350px;">
  <option value="">Please Select</option>
  <option value="Dog">Dog</option>
  <option value="Cat">Cat</option>
  <option value="Cow">Cow</option>
  <option  value="Rat">Rat</option>
</select>

You can save it in the url, than refresh the page with the new url, and take the value from url to be the selected value in the drop down list. 您可以将其保存在url中,然后用新的url刷新页面,然后将url中的值用作下拉列表中的选定值。

Ex. 例如

window.location.href = window.location.href + '/' + $('select').val();
$('input').text() = window.location.href.split('/').last();

Try this: 尝试这个:

<?php
$options = array("Dog", "Cat", "Cow", "Rat");
?>

<select name="animal" style="width: 350px;">
  <option value="">Please Select</option>
<?php
  foreach($options as $option) {
    $selected = $POST['animal'] == $option ? 'selected' : ''; 
    echo '<option $selected value="$option">$option</option>';
  }
?>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM