PHP/HTML/Javascript:如何为 div 元素添加值?
我的问题是:向下拉列表中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个 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/