0

I have a little problem where I need to generate a poll/survey template from a list of answers to be used in an email newsletter.

So - The poll is created in the CMS and answers are displayed in an ordered list. There's a text field in the list item where users input their answer. - Beneath the ordered list is a textarea with a button called "generate poll code" right beneath it - On clicking the button, javascript should generate the desired "template" with the Answers, Answer Ids and the Poll Id in anchor links with a query (as shown below).

So the ordered list markup is as following:

<ol id="9999">
    <li id="1234"><input value="Answer1"/></li>
    <li id="5678"><input value="Answer1"/></li>
    <li id="9876"><input value="Answer1"/></li>
</ol>

Where the poll ID is the id of the ordered list and each li has the answer ID.

What I've been trying to get is this simple list of anchor links:

Poll question<br />
<a href="http://urlpath?pollId=9999&answerId="1234">Answer 1</a>
<a href="http://urlpath?pollId=9999&answerId="5678">Answer 1</a>
<a href="http://urlpath?pollId=9999&answerId="9876">Answer 1</a>

So basically, on click, iterate over the ordered list, grab the ids and then output the anchor list with the ids in the right places.

Just reduce this email, I haven't included my unsuccessful attempts at this, where I tried to use a for loop, get the Ids, store them in an array and then build the anchor link template with those injected.

Is there anyone who knows of good way to handle this and get the desired results

Very grateful for any assistance, THANK YOU!

  • L
4

1 回答 1

0

您可以使用 .each 遍历每个列表项并输出每个项目的 html。

    //use .each to loop over each li
    $('#9999 li').each(function(){
        //for each li, get its id and val
        var li_id = $(this).attr('id');
        var inp_val = $(this).find('input').val();
        //append it somewhere and drop values in
        $('#output').append('<a href="http://urlpath?pollId=9999&answerId=' + li_id + '">' + inp_val + '</a><br>' );
    });

工作示例:http: //jsfiddle.net/omgo/fThq9/2/

于 2013-07-25T12:51:32.953 回答