繁体   English   中英

使用jQuery和JavaScript动态检测多个下拉选择和段落内容

[英]Detect multiple drop-down selections and paragraph content dynamically with jQuery and JavaScript

我创建了一个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();
});
  1. 您正在基于" " (using space)分割文本" " (using space) ,并将元素{替换为array值,但text is. {, and text is. {, and包含逗号,即{,不等于{ 在元素{之后添加空格。 这解决了您的第一个问题。

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

尝试:

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

代替

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

演示场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM