通过多个链接发布或获取多个值

发布于 2024-12-15 07:11:12 字数 501 浏览 1 评论 0原文

我是新手,所以请耐心等待...

我希望能够使用链接将多个变量传递到另一个页面,例如:

<a href="value1">some text</a>,<a href="value2">some text2</a>,<a href="value3">some text3</a>,<a href="value4">some text4</a>

我希望能够将其用作“多重选择器”,以便用户将能够单击(选择)他们想要的任何文本,并以某种方式使用提交按钮通过 post 或 get 发送这些选定的值。

我不想使用菜单或列表,因为我试图显示文本,并且每个链接包含文本的不同部分,例如章节中的段落,因此用户会单击一个段落(这是一个链接,或者至少看起来像一个)并将其值发送到其他页面,但是我想要多个段落,以便用户能够选择段落,如果可能的话取消选择段落。

如果可能的话希望有人能指出我正确的方向

I'm new with this so please be patient with me...

I want to be able to pass multiple variables to another page using links, for example:

<a href="value1">some text</a>,<a href="value2">some text2</a>,<a href="value3">some text3</a>,<a href="value4">some text4</a>

I want to be able to use this as "multi-selector" so that the user would be able to click (select) what ever text they want and somehow with a submit button send those selected values via post or get.

I don't want to use a menu or list as I'm trying to show text and each link holds different parts of the text, kind of parragraphs in a chapter, so the user would click a paragraph (wich is a link, or at least looks like one) and send it's value to other page, however I want multiple parragraphs so that the user would be able to select and if possible unselect paragraphs.

Hope someone would point me to the right direction if possible

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

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

发布评论

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

评论(2

超可爱的懒熊 2024-12-22 07:11:12

以下是我可能如何实现这一点:

HTML

<a data-id="value1" href="value1">some text</a>,<a data-id="value2" href="value2">some text2</a>,<a data-id="value3" href="value3">some text3</a>,<a data-id="value4" href="value4">some text4</a>
<input type="button" id="submit" value="submit"></input>

Javascript:

var els = document.getElementsByTagName("a");
var sz = els.length;

var o = {};
for(var n = 0; n < sz; ++n) {
    els[n].onclick = function(e) {
        e.preventDefault();
        var s = e.currentTarget.getAttribute("data-id");
        if(o[s] !== undefined) {
            delete o[s];
        } else {
            o[s] = "";
        }
    };
}
document.getElementById("submit").onclick = function(e) {
    var s = "";
    for(key in o) {
        s += key + "&";
    }
    var location = s.substring(0, s.length-1);
    // redirect user using location as parameter list or send ajax request
};

(我在上面使用地图而不是数组,以避免每次点击时都必须遍历数组)

http://jsfiddle.net/5y7wK/

Here's how I might go about implementing this:

HTML:

<a data-id="value1" href="value1">some text</a>,<a data-id="value2" href="value2">some text2</a>,<a data-id="value3" href="value3">some text3</a>,<a data-id="value4" href="value4">some text4</a>
<input type="button" id="submit" value="submit"></input>

Javascript:

var els = document.getElementsByTagName("a");
var sz = els.length;

var o = {};
for(var n = 0; n < sz; ++n) {
    els[n].onclick = function(e) {
        e.preventDefault();
        var s = e.currentTarget.getAttribute("data-id");
        if(o[s] !== undefined) {
            delete o[s];
        } else {
            o[s] = "";
        }
    };
}
document.getElementById("submit").onclick = function(e) {
    var s = "";
    for(key in o) {
        s += key + "&";
    }
    var location = s.substring(0, s.length-1);
    // redirect user using location as parameter list or send ajax request
};

(I use a map rather than an array in the above to avoid having to traverse an array on every click)

http://jsfiddle.net/5y7wK/

橘和柠 2024-12-22 07:11:12

传递多个值的最佳方法是通过表单提交或 AJAX 请求,您可以有多个复选框,以便当用户选择所需的值然后在单个请求中发送它们时。

这是一个示例:示例



<form action="/myurl" method="POST">
        <input id="Val1" name="paragraph" type="checkbox" value="value1"/>
        <label for="Val1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
        In egestas tempus dictum. Mauris purus urna, congue non scelerisque non,
        feugiat at arcu. Donec venenatis facilisis fermentum. Morbi ac lorem odio. 
        Cras nulla justo, pharetra et placerat ut, sagittis nec urna. 
        Praesent luctus, sem ac lobortis aliquam, purus sapien tempor dui, nec 
        pretium metus leo eu ligula. Cras ac egestas dolor. 
        </label>
        </br>
        </br>
        <input id="Val2" name="paragraph" type="checkbox" value="value2"/>
        <label for="Val2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
        In egestas tempus dictum. Mauris purus urna, congue non scelerisque non,
        feugiat at arcu. Donec venenatis facilisis fermentum. Morbi ac lorem odio. 
        Cras nulla justo, pharetra et placerat ut, sagittis nec urna. 
        Praesent luctus, sem ac lobortis aliquam, purus sapien tempor dui, nec 
        pretium metus leo eu ligula. Cras ac egestas dolor. </label>
        </br>
        <input type="submit"/>
    </form>

The best way to pass multiple values is through a form submission or an AJAX request, you could have multiple check boxes so that when the user selects the values it wants and then sends them in a single request.

Heres a sample : Sample



<form action="/myurl" method="POST">
        <input id="Val1" name="paragraph" type="checkbox" value="value1"/>
        <label for="Val1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
        In egestas tempus dictum. Mauris purus urna, congue non scelerisque non,
        feugiat at arcu. Donec venenatis facilisis fermentum. Morbi ac lorem odio. 
        Cras nulla justo, pharetra et placerat ut, sagittis nec urna. 
        Praesent luctus, sem ac lobortis aliquam, purus sapien tempor dui, nec 
        pretium metus leo eu ligula. Cras ac egestas dolor. 
        </label>
        </br>
        </br>
        <input id="Val2" name="paragraph" type="checkbox" value="value2"/>
        <label for="Val2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
        In egestas tempus dictum. Mauris purus urna, congue non scelerisque non,
        feugiat at arcu. Donec venenatis facilisis fermentum. Morbi ac lorem odio. 
        Cras nulla justo, pharetra et placerat ut, sagittis nec urna. 
        Praesent luctus, sem ac lobortis aliquam, purus sapien tempor dui, nec 
        pretium metus leo eu ligula. Cras ac egestas dolor. </label>
        </br>
        <input type="submit"/>
    </form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文