简单的 html/javascript

发布于 2025-01-07 01:11:12 字数 757 浏览 1 评论 0原文

基本上,我有一堆图像,当我单击图像时,它会将图像 ID 发送到 JavaScript 函数,该函数设置一个值,然后在我提交表单时对其进行处理,以便它进入下一页(我可以在 Coldfusion 中查询该值)。

哎呀,忘记了这个问题:基本上,它也没有达到我想要的效果。带有值的变量似乎没有被发送到下一页!

<html>
<head>

<Script type="text/javascript">

function recordClick(imageid)
{ 
var myfield = document.getElementById("numSend").value = imageid
}
</script>

</head>

<body>
<FORM action="nextPage.cfm" method="post">

<img src="1.png" NAME="image1" onclick="recordClick(1)"
<img src="2.png" NAME="image2" onclick="recordClick(2)"
<img src="3.png" NAME="image3" onclick="recordClick(3)"

<input type="hidden" id="numSend"/>
<input type="submit" value="Done"/>
</FORM>

</body>
</html>

Basically, i have a bunch of images, and when I click on an image it sends the image ID, to a javascript function, that sets a value which is then processed later when I submit the form so that it goes to the next page (where I can query that value in coldfusion).

Oops, forgot the question: Basically, it doesnt do what I want it too. The variable with the values are not being sent to the next page it seems!

<html>
<head>

<Script type="text/javascript">

function recordClick(imageid)
{ 
var myfield = document.getElementById("numSend").value = imageid
}
</script>

</head>

<body>
<FORM action="nextPage.cfm" method="post">

<img src="1.png" NAME="image1" onclick="recordClick(1)"
<img src="2.png" NAME="image2" onclick="recordClick(2)"
<img src="3.png" NAME="image3" onclick="recordClick(3)"

<input type="hidden" id="numSend"/>
<input type="submit" value="Done"/>
</FORM>

</body>
</html>

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

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

发布评论

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

评论(2

哆兒滾 2025-01-14 01:11:12

您的 标签未正确关闭。此外,您的隐藏输入没有 name 属性,因此当您提交表单时,不会向服务器发送任何内容。尝试这样:

<html>
<head>
    <title>test</title>
    <script type="text/javascript">
    function recordClick(imageid) { 
        var numSend = document.getElementById("numSend");
        numSend.value = numSend + imageid.toString();
    }
    </script>
</head>
<body>
    <form action="nextPage.cfm" method="post">
        <img src="1.png" name="image1" onclick="recordClick(1)" alt="" />
        <img src="2.png" name="image2" onclick="recordClick(2)" alt="" />
        <img src="3.png" name="image3" onclick="recordClick(3)" alt="" />

        <input type="hidden" id="numSend" name="numSend" />
        <input type="submit" value="Done" />
    </form>
</body>
</html>

Your <img> tags are not properly closed. Also your hidden input doesn't have a name attribute so nothing will be sent to the server when you submit the form. Try like this:

<html>
<head>
    <title>test</title>
    <script type="text/javascript">
    function recordClick(imageid) { 
        var numSend = document.getElementById("numSend");
        numSend.value = numSend + imageid.toString();
    }
    </script>
</head>
<body>
    <form action="nextPage.cfm" method="post">
        <img src="1.png" name="image1" onclick="recordClick(1)" alt="" />
        <img src="2.png" name="image2" onclick="recordClick(2)" alt="" />
        <img src="3.png" name="image3" onclick="recordClick(3)" alt="" />

        <input type="hidden" id="numSend" name="numSend" />
        <input type="submit" value="Done" />
    </form>
</body>
</html>
半暖夏伤 2025-01-14 01:11:12

我发现代码存在一个问题 - 隐藏字段没有名称属性,因此不会发送该值。

但除此之外,不使用 JavaScript,只使用纯 HTML + CSS 也可以实现相同的结果。只要看看这个小提琴: http://jsfiddle.net/VVD3y/

如果你不点击任何图标,您将被重定向到 google。如果您选择其中一个图标并单击“完成”,您将被重定向到带有查询 stackoverflow、google 或 mozilla 的 google。只需更改每个单选按钮的 NAME 属性,然后在 VALUE 属性中输入您的图像 ID。

如果您想发送多个值,只需将单选按钮替换为复选框即可。

I see one problem with the code – the hidden field doesn't have a name attribute, so the value won't be sent.

But apart from that, there is a possibility to achieve the same result without using JavaScript, but only plain HTML + CSS. Just look at this fiddle: http://jsfiddle.net/VVD3y/

if you don't click any icon, you will be just redirected to google. If you select one of the icons and click Done, you will be redirected to google with the query stackoverflow, google or mozilla. Just change the NAME attribute of every radio button, and in VALUE attributes put your image IDs.

If you'd like to send multiple values, just replace the radio buttons with checkboxes.

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