简体   繁体   English

在div标签中选择文本区域

[英]Selecting textareas within div tags

feel like im coming here way too often to ask questions but yet again I am stuck. 感觉我经常来这里问问题,但我再次陷入困境。 I am attempting to select a textarea and allow myself to edit the text in another textarea, which works fine using textboxs but not with textareas. 我正在尝试选择一个文本区域,并允许我自己编辑另一个文本区域中的文本,这在使用文本框时效果很好,但在文本区域中却不能。 Every time I click on the div container I am getting an undefined result when looking for the textarea. 每次单击div容器时,在查找textarea时都会得到不确定的结果。 Below is the code. 下面是代码。

jQuery jQuery的

     $(".textAreaContainer").live('click','div', function(){
        var divID = this.id;
        if ( divID !== "" ){

            var lastChar = divID.substr(divID.length - 1);
            var t = $('#' + divID ).find(':input');
            alert(t.attr('id'));

            t = t.clone(false);               
            t.attr('data-related-field-id', t.attr('id'));              
            t.attr('id', t.attr('id') + '_Add');
            t.attr('data-add-field', 'true');

            var text = document.getElementById(divID).innerHTML;
            //var textboxId = $('div.textAreaContainer').find('input[type="textArea"]')[lastChar].id;
            $('div#placeholder input[type="button"]').hide();
            var text = "<p>Please fill out what " + t.attr('id') +" Textarea shall contain</p>";

            if ( $('#' + t.attr('id')).length == 0 ) {
                $('div#placeholder').html(t);
                $('div#placeholder').prepend(text);
            }
        }
        else{

        }
    });

t.attr('id') should be returning textbox1(or similar) but instead just returns undefined. t.attr('id')应该返回textbox1(或类似值),而只是返回未定义。

I have tried .find(':textarea'),.find('textarea'),.find(text,textArea),.find(':input') and quite a few others that I have found through google but all of them return undefined and I have no idea why. 我已经尝试过.find(':textarea')、. find('textarea')、. find(text,textArea)、. find(':input')和我通过Google找到的很多其他功能,他们返回未定义,我不知道为什么。 A demo can be found here, http://jsfiddle.net/xYwaw/ . 可以在http://jsfiddle.net/xYwaw/中找到一个演示。 Thanks in advance for any help guys, it is appreciated. 在此先感谢您的帮助,我们非常感谢。

EDIT: Below is the code for a very similar example I am using. 编辑:以下是我正在使用的非常相似的示例的代码。 This does what I want to do but with textboxs instead of textareas. 这是我想要做的,但是使用文本框而不是textareas。

     $('#textAdd').live('click',function() {
        var newdiv = document.createElement('div');
        newdiv.innerHTML = "Textbox " + textBoxCounter + " <br><div id='container" + counter + "' class='container'><li><input type='text' id='textBox" + textBoxCounter +"' name='textBox" + textBoxCounter + "'></li></div></br>";
        document.getElementById("identifier").appendChild(newdiv);
        textBoxCounter++
        counter++;
    });
    $(".container").live('click','div', function(){
        var divID = this.id;
        if ( divID !== "" ){
            var lastChar = divID.substr(divID.length - 1);
            var t = $('#' + divID).find('input');
            alert(divID);


            t = t.clone(false);
            t.attr('data-related-field-id', t.attr('id'));
            alert(t.attr('id'));
            t.attr('id', t.attr('id') + '_Add');               
            t.attr('data-add-field', 'true');


            var text = document.getElementById(divID).innerHTML;
            // var textboxId = $('div.container').find('input[type="text"]')[lastChar].id;
            $('div#placeholder input[type="button"]').hide();
            var text = "<p>Please fill out what " + t.attr('id') +" textbox shall contain</p>";

            if ( $('#' + t.attr('id')).length == 0 ) {
                $('div#placeholder').html(t);
                $('div#placeholder').prepend(text);
            }
        }
        else{

        }
    });

First up remove the second parameter, 'div', from the first line: 首先,从第一行中删除第二个参数“ div”:

$(".textAreaContainer").live('click','div', function(){

...to make it: ...使其:

$(".textAreaContainer").live('click', function(){

Then change: 然后更改:

var t = $('#' + divID ).find(':input');

...to: ...至:

var t = $(this).find(':input');

Because you already know that this is the container so there's no need to select it again by id. 因为您已经知道this是容器,所以无需再次通过id选择它。 Also the id attributes that you're assigning to your textarea containers have a space in them, which is invalid and results in your original code trying to select the element with '#textAreaContainer 0' which actually looks for a 0 tag that is a descendant of #textAreaContainer . 另外,您要分配给textarea容器的id属性在其中具有空格,这是无效的,并且会导致原始代码尝试选择具有'#textAreaContainer 0'的元素,而该元素实际上会寻找作为后代的0标签的#textAreaContainer So fixing the code that creates the elements to remove that space in the id is both a good idea in general and an alternative way of fixing this problem. 因此,修复创建可删除id中的空格的元素的代码通常是一个好主意,也是解决此问题的另一种方法。

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

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