简体   繁体   English

HTML网站显示选项

[英]HTML website display option

I am a beginner in HTML programming and I have the following question. 我是HTML编程的初学者,我有以下问题。 Im working on a simple try-out. 我正在进行简单的试用。

I have following selection menu: 我有以下选择菜单:

<p>Kies de frisdrank:</p>
<select id="frisdrank">
  <option value="Cola"> Cola </option>
  <option value="Fanta"> Fanta </option>
  <option value="spuitwater"> spuitwater </option>
  <option value="chocolademelk"> chocolademelk </option>
</select>

I also have a button: 我也有一个按钮:

    <input id="OK" type="button" value="OK">

Now how do I display a text somewhere or a label that says what option is selected when I press the button? 现在,我如何在某个地方显示文字或显示按下按钮时选择了哪个选项的标签? I understand this is very basic but still I cant handle to do this. 我知道这是非常基本的,但我仍然无法做到这一点。

** UPDATE!!! **更新!!! ** **

Thanks everyone already for the help! 谢谢大家的帮助! Now I figured out how to do this. 现在我想出了如何做到这一点。 Here is my followup question : Can I add another textfield with a matching price? 这是我的后续问题:我可以添加另一个匹配价格的文本字段吗? Like if you select Cola then the price is for example 20. And If you select fanta the price is 25? 就像你选择Cola然后价格是例如20.如果你选择fanta,价格是25?

I figured out the javascript. 我想出了javascript。 I already have a little bit experience with normal Java. 我已经对普通Java有一点经验了。

Thanks for helping me out! 谢谢你的协助!

I suggest you to try jQuery: 我建议你试试jQuery:

 $("#OK").click(function() { $("#value").text($("#frisdrank").val()); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <p>Kies de frisdrank:</p> <select id="frisdrank"> <option value="Cola"> Cola </option> <option value="Fanta"> Fanta </option> <option value="spuitwater"> spuitwater </option> <option value="chocolademelk"> chocolademelk </option> </select> <input id="OK" type="button" value="OK"> <span id="value"></span> 

just to explain 只是为了解释
1. onclick= to attach a onClick event to your button and it will run the function inside of onClick, this case myFunction. 1. onclick=将onClick事件附加到您的按钮,它将运行onClick内部的函数,本例为myFunction。
2. use getElementById to get the element you want 2.使用getElementById获取所需的元素
3. use .value to get the value of the selected option 3.使用.value获取所选选项的值
4. you can use that value whatever you want. 4.您可以随意使用该值。 use innerHTML to place in body. 使用innerHTML放置在体内。

 function myFunction() { var selectElement = document.getElementById('selectId'); var selectValue = selectElement.value; alert(selectValue); } 
 <select id='selectId'> <option>1</option> <option>2</option> </select> <button id='buttonId' onclick='myFunction();'>click</button> 

 function a() { var obj = document.getElementById('frisdrank'); document.getElementById('selected').innerHTML = obj.options[obj.selectedIndex].innerHTML; } 
 <p>Kies de frisdrank:</p> <select id="frisdrank"> <option value="Cola"> Cola </option> <option value="Fanta"> Fanta </option> <option value="spuitwater"> spuitwater </option> <option value="chocolademelk"> chocolademelk </option> </select> <input id="OK" type="button" value="OK" onclick="a();"> <br><span id="selected">hi</span> 

try this 尝试这个

<p>Kies de frisdrank:</p>
<select id="frisdrank">
    <option value="Cola">Cola</option>
    <option value="Fanta">Fanta</option>
    <option value="spuitwater">spuitwater</option>
    <option value="chocolademelk">chocolademelk</option>
</select>
<input id="OK" type="button" value="OK" onclick="foo()">
<div id="frisdrankSelected"></div>
    <script>
        function foo() {
        var e = document.getElementById("frisdrank");
        var strUser = e.options[e.selectedIndex].value;
        document.getElementById("frisdrankSelected").innerHTML = strUser;
        }
    </script>

the function foo will replace the content of the div with id frisdrankSelected with the text selected from the dropdown 函数foo将使用从下拉列表中选择的文本替换id为frisdrankSelected的div的内容

here the jsfiddle to test it: https://jsfiddle.net/m4mphbgc/ 在这里测试它的jsfiddle: https ://jsfiddle.net/m4mphbgc/

You can also use PHP. 你也可以使用PHP。

JavaScript will run on the computer and PHP will run on the server. JavaScript将在计算机上运行,​​PHP将在服务器上运行。 If you don't want the user to read your code; 如果您不希望用户阅读您的代码; don't use JavaScript.. 不要使用JavaScript ..

Example with PHP: PHP示例:

<form method="POST">
    <select id="frisdrank" name="selection">
        <option value="Cola"> Cola </option>
        <option value="Fanta"> Fanta </option>
        <option value="spuitwater"> spuitwater </option>
        <option value="chocolademelk"> chocolademelk </option>
    </select>

    <input id="OK" type="submit" value="OK">
</form>

<?php
    if(isset($_POST['selection'])){
        echo "You have selected".$_POST['selection'];
    }
?>

( echo will print out the result and $_POST is a variable) echo将打印出结果, $_POST是一个变量)

To get PHP working on your computer, you will need to create a local server (or you can upload the code to a server). 要让PHP在您的计算机上运行,​​您需要创建一个本地服务器(或者您可以将代码上传到服务器)。

Please compare the languages Read here 请比较语言在这里阅读

You can have price associated as follow: JavaScript code: 您可以按以下方式关联价格:JavaScript代码:

  $("#OK").click(function() {
      Var comboValue = $("#frisdrank").val();
     $("#value").text(comboValue);
      If(comboValue == "Cola")
                 $("#price").text("10");
      Else  If(comboValue == "Fanta")
                 $("#price").text("12");  
       . .  . . . .
      });

HTML code will looks like this: HTML代码如下所示:

   <script src="https://    ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p>Kies de frisdrank:</p>
<select id="frisdrank">
 <option value="Cola"> Cola </option>
 <option value="Fanta"> Fanta </option>
 <option value="spuitwater"> spuitwater          </option>
 <option value="chocolademelk">  chocolademelk </option>
 </select>
<input id="OK" type="button" value="OK">
    Selected Drink: <span id="value"></span> </br>
     Price:<span id="price"></span>

Simply you can have an element on HTML and on some event you can change value of that element or even you can create new elements dynamically using javascript. 只需在HTML上有一个元素,在某些情况下,您可以更改该元素的值,甚至可以使用javascript动态创建新元素。

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

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