PHP/HTML/Javascript:如何为 div 元素添加值?

发布于 2024-09-07 20:20:29 字数 1702 浏览 3 评论 0原文

我的问题是:向下拉列表中的 div 元素添加一个值。

好的,伙计们,我已经在@David 的帮助下解决了我的问题。

我一直想知道如何设计

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>

    function getValue(value){
            var valueHolder = document.getElementById('valueHolder');
            valueHolder.value = value;
    }

    function toggle(id){
        var element = document.getElementById(id);
        if(element.style.display == "none"){
            element.style.display = "block";
        } else {
            element.style.display = "none";
        }
    }


</script>
</head>

<body>

<form action="test2.php" method="post">

    <input type="hidden" id="valueHolder" value="" name="valueHolder"></input>

    <div onclick="toggle('dropdown');">Show Dropdown</div>

    <div class="dropdown" id="dropdown" style="display:none;">
        <div class="option" onclick="getValue('red')">Red</div>
        <div class="option" onclick="getValue('green')">Green</div>
    </div>  

    <input type="submit" name="submit" />  
    <?php 
        if(isset($_POST['submit'])){
            echo $_POST['valueHolder'];
        }
    ?>
</form>
</body>
</html>

它有点难看,但检查一下,有效!不要忘记在服务器上运行。

此致, 亚当

My Problem was: Add a value to a div element in a dropdown list.

Ok Guys, I've resolved my problem with the help of @David.

I've always wanted to know how to design the <select> <option> tags, but now I think I found a solution to get away from them and use whatever element I want. Ok here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>

    function getValue(value){
            var valueHolder = document.getElementById('valueHolder');
            valueHolder.value = value;
    }

    function toggle(id){
        var element = document.getElementById(id);
        if(element.style.display == "none"){
            element.style.display = "block";
        } else {
            element.style.display = "none";
        }
    }


</script>
</head>

<body>

<form action="test2.php" method="post">

    <input type="hidden" id="valueHolder" value="" name="valueHolder"></input>

    <div onclick="toggle('dropdown');">Show Dropdown</div>

    <div class="dropdown" id="dropdown" style="display:none;">
        <div class="option" onclick="getValue('red')">Red</div>
        <div class="option" onclick="getValue('green')">Green</div>
    </div>  

    <input type="submit" name="submit" />  
    <?php 
        if(isset($_POST['submit'])){
            echo $_POST['valueHolder'];
        }
    ?>
</form>
</body>
</html>

It's a bit ugly but check it out, works! Don't forget to run on a server.

Best Regards,
Adam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2024-09-14 20:20:29

每个 div 可能都需要自己的单击事件来获取其内部文本并将其存储在隐藏的表单字段中。这可以通过 HTML 之外的 jQuery 更优雅地完成,以便将其与内容分开。

jQuery 点击事件: http://api.jquery.com/click/

获取元素的 jQuery 方法文本: http://api.jquery.com/text/

Each div is probably going to need its own click event to grab its inner text and store it in a hidden form field. This can be more elegantly accomplished with jQuery outside of the HTML so as to separate it from the content.

jQuery click event: http://api.jquery.com/click/

jQuery method for getting element's text: http://api.jquery.com/text/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文