1

我创建了一个 madlib 风格的段落,其中包含多个下拉选择,用于各种单词的同义词。这是一个例子:

<p id="the-text">This is an example paragraph containing many
    <select class="selector">
        <option>selections</option>
        <option>dropdown thingies</option>
        <option>option choosers</option>
    </select>that I would like to be able to
    <select class="selector">
        <option>click on</option>
        <option>select</option>
        <option>choose</option>
    </select>and then know what the
    <select class="selector">
        <option>final</option>
        <option>selected</option>
        <option>variable</option>
    </select>paragraph text is.
    <select class="selector">
        <option>It would be great</option>
        <option>It would be nice</option>
        <option>It'd be delightful</option>
    </select>, and
    <select class="selector">
        <option>useful</option>
        <option>helpful</option>
        <option>interesting</option>
    </select>to dynamically create paragraphs like this.</p>
<textarea id="text-area" rows="4" cols="110">This is where the text should appear...     
</textarea>

这是一个活生生的例子:http: //jsfiddle.net/T4guG/2/

使用 jQuery 和 Javascript,我试图让选定的(和周围的)文本出现在文本区域中。

这有点工作,但有两个问题:

1)已解决:标点符号有问题,但替换:

    if (element == "{") {
        content_array[i] = foo[j];
        j++;
    }

    if (element.indexOf('{') >= 0) {
        content_array[i] = foo[j];
        j++;
    }

允许 { 被一致地检测到

2) 已解决:您只能更改一次选项。

有没有比我想出的更优雅的解决方案?这是代码:

function updateTextArea() {
    //get all of the text selections, and put them in an array
    var foo = [];
    $('.selector :selected').each(function (i, selected) {
        foo[i] = $(selected).text();
    });

    //get the paragraph content, and store it
    var safe_content = $('#the-text').html();

    //delete all the options
    $('.selector').text('');

    //get the text without the dropdown options
    var content = $('#the-text').html();

    //create a regex expression to detect the remaining drop-down code
    var pattern = "<select class=\"selector\"></select>",
        re = new RegExp(pattern, "g");

    //replace all the drop-down selections with {
    content = content.replace(re, "{");

    //turn the content into an array
    content_array = content.split(" ");

    //go through the array, and if a element is {, go to "foo" and replace it with the selected option
    var length = content_array.length,
        element = null;
    var j = 0;
    for (var i = 0; i < length; i++) {
        element = content_array[i];

        if (element == "{") {
            content_array[i] = foo[j];
            j++;
        }

    }

    //turn the array back into a paragraph
    new_content = content_array.join(" ");

    //replace the text with the origionanl text
    $('#the-text').html(safe_content);

    //put the new content into the text area
    $('#text-area').val(new_content);
}

$(document).ready(function () {
    updateTextArea();
});

$(".selector").change(function () {
    updateTextArea();
});
4

1 回答 1

2
  1. 您正在根据元素拆分文本并用值" " (using space)替换元素,但 包含逗号,即不等于。在元素之后添加空格。这解决了你的第一个问题。{arraytext is. {, and{,{{

  2. 当您select在 function 中动态删除和添加选项时updateTextArea()。您必须使用.on()为动态创建的元素附加事件处理程序。

尝试:

$( document ).on("change",".selector",function() {
  updateTextArea();
});

代替

$(".selector").change(function () {
    updateTextArea();
});

DEMO FIDDLE

于 2013-09-29T06:29:06.650 回答