简体   繁体   English

带有textarea内容的警报(HTML到JavaScript)

[英]Alert with a textarea content (HTML to JavaScript)

I'm new at coding with any language and now im working (trying to) with HTML, PHP and JavaScript. 我是使用任何语言进行编码的新手,现在正在使用HTML,PHP和JavaScript工作(尝试)。 My difficulty is to show through an alert in a JavaScript file the information that is in a textarea from an HTML file. 我的难点是通过JavaScript文件中的警报显示来自HTML文件的textarea中的信息。 The HTML code is this: HTML代码是这样的:

    <head>
        <title> My Form</title>
        <script type= "text/javascript" src ="./JavaScript/validaLinha.js"></script>
    </head>
    <body>
        <form name="Linhas" method="POST" action="Linhas-p.php">
             <textarea name="descricaoLinha" onFocus="resetField('descricaoLinha')"> TEXT-TEXT-TEXT</textarea>
        </form>
    </body>

As you can see at the HTML code, I try to send the name of the textarea to a JavaScript function called "resetField". 正如您在HTML代码中看到的,我尝试将textarea的名称发送到名为“resetField”的JavaScript函数。 Let's see what the "resetField" does: 让我们看看“resetField”的作用:

function resetField(field){
    d = document.Linhas;

    alert("It's entering the function."); //ANSWER = It's entering the function.
    alert(field);                         //ANSWER = descricaoLinha
    alert(d.field.value);                 //ANSWER = Nothing.
    alert(d.getElementById(field).value); //ANSWER = Nothing.
}

I can't get the information set at the value of my textarea! 我无法获得设置为textarea值的信息! The event is calling the function (First alert shows up), the string is being received by the function (Second alert shows the name of the text area) but the other two that are the ones that i need doesn't show up. 该事件正在调用该函数(第一个警报显示),该函数正在接收该字符串(第二个警报显示文本区域的名称),但是我需要的其他两个没有显示。 I already tried to change the order of the alerts because of that JavaScript stuff that doesn't continues to read the code if an error appears. 我已经尝试更改警报的顺序,因为如果出现错误,那些不会继续读取代码的JavaScript内容。

Just to emphasize, i want the content IN the textarea. 只是为了强调,我想要textarea中的内容。 (The name of it is being received properly) (它的名称正在接收)

Thanks for reading! 谢谢阅读! Sorry about my english. 抱歉我的英语。 ^^ ^^

Add an ID to your textaea 在textaea中添加ID

<textarea id="descricaoLinha" name="descricaoLinha" onFocus="resetField('descricaoLinha')"> TEXT-TEXT-TEXT</textarea>

And then, send the ID instead of name 然后,发送ID 而不是 name

alert(d.getElementById(field).value);

This code can't find the element descricaoLinha because its searching for an element that has an ID = " descricaoLinha " 此代码找不到元素descricaoLinha,因为它搜索具有ID =“ descricaoLinha ”的元素

Your 'field' is just a string, not a javascript object and of course it does not have the name attribute; 你的“字段”只是一个字符串,而不是一个javascript对象,当然它没有name属性;

And it is similar to the d = document.linha; 它类似于d = document.linha;

You have have to get the DOM element by its id attribute: 您必须通过其id属性获取DOM元素:

d = document.getElementById("Linha"); d = document.getElementById(“Linha”);


Edit: You also need add the id attribute to your field 编辑:您还需要将id属性添加到您的字段

<textarea id="descricaoLinha" name="descricaoLinha" onFocus="resetField('descricaoLinha')"> ...

One solution is: you change your JavaScript function to 一种解决方案是:将JavaScript函数更改为

function resetField(fieldId) {
    var field = document.getElementById(fieldId);
    alert(field);           // object
    alert(field.value);     // text field value    
    // Do whatever you want with the field object here 
}

You should turn on the console and using console.log('output') instead of using alert() for debugging; 您应该打开控制台并使用console.log('output')而不是使用alert()进行调试; You can turn on Firefox console by Ctrl + Shift + K 您可以通过Ctrl + Shift + K打开Firefox控制台

You should pass in this in your onfocus handler: 你应该通过this在你onfocus处理程序:

onFocus="resetField(this);"

Access it like: 访问它像:

alert("It's entering the function."); 
alert(field.name);                       
alert(field.value);

Here's a demo: http://jsfiddle.net/RbUZr/ 这是一个演示: http//jsfiddle.net/RbUZr/

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

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