Javascript 在表单中注册已单击的图像
我在一个HTML文档中有多个图像,我希望它们在单击时呈现唯一的值(以某种可检索的方式)。我尝试将它们作为表单元素,如下所示:Javascript 在表单中注册已单击的图像,javascript,php,html,forms,Javascript,Php,Html,Forms,我在一个HTML文档中有多个图像,我希望它们在单击时呈现唯一的值(以某种可检索的方式)。我尝试将它们作为表单元素,如下所示: <form id="myform" method="post" action=""> <input type="hidden" name="action" value="submit" /> <div class="flex-item"><input type="image" name="submit" value
<form id="myform" method="post" action="">
<input type="hidden" name="action" value="submit" />
<div class="flex-item"><input type="image" name="submit" value="alt1" alt="alt1" src="images/<?php echo $data[$counter] ?>"></div>
<div class="flex-item"><input type="image" name="submit" value="alt2" alt="alt2" src="images/<?php echo $data[$counter+1] ?>"></div>
</form>
使用代码时,您会在$\u POST
对象中获得submit
参数(因为按钮的属性name
)。该值将是值
属性
所以你可以这样检查:
HTML/CSS-唯一的方式
设置CSS以隐藏单选按钮:
.hidden {
display: none !important;
}
<form method="post" name="myForm">
<div>
<input type="radio" name="image" value="image1" id="image1" class="hidden">
<label for="image1"><img src="path-to-your-image.jpg"></label>
</div>
<div>
<input type="radio" name="image" value="image2" id="image2" class="hidden">
<label for="image2"><img src="path-to-your-other-image.jpg"></label>
</div>
<div>
<input type="submit" name="save" value="Save Image Selection">
</div>
</form>
在表单中,使用单选按钮跟踪选择的图像。将图像放在相关单选按钮的标签内。确保在单选按钮的value
属性中放入PHP中所需的任何信息:
.hidden {
display: none !important;
}
<form method="post" name="myForm">
<div>
<input type="radio" name="image" value="image1" id="image1" class="hidden">
<label for="image1"><img src="path-to-your-image.jpg"></label>
</div>
<div>
<input type="radio" name="image" value="image2" id="image2" class="hidden">
<label for="image2"><img src="path-to-your-other-image.jpg"></label>
</div>
<div>
<input type="submit" name="save" value="Save Image Selection">
</div>
</form>
请尝试type=“hidden”
它也应该在单击时提交。您可能需要添加一些Javascript,它将侦听对图像的单击,并将更新假设的@cale\b jQuery的值,尽管我是新手。图像类型似乎发送信息,而不是您需要的信息。它发送我签入代码时单击的按钮的X和Y坐标。尝试将我的代码复制/粘贴到那里并检查。我可能遗漏了一些内容,但在phpfiddle中也无法正常工作。我无法让Javascript正常工作。我需要下载jQuery吗?是的,您需要在页面中加载jQuery。您可以将它放在您的
:
-这将从jQuery CDN()加载它。或者你可以下载它并链接到你下载的副本。哦,太好了,谢谢!我下载并链接了。(你发布的链接代码对我来说不起作用)但是好极了,现在一切都正常了!
$image = $_POST[ 'image' ]; // 'image' is the name of the radio buttons
var_dump( $image );
// Will result in "image1" or "image2", etc - whatever "value" you assigned to the radio buttons