简体   繁体   English

使用JavaScript在Textarea中显示表单结果

[英]Display Form Results in Textarea with JavaScript

I'm new to programming. 我是编程新手。 I've made a form that displays results in a HTML textarea with javascript. 我已经制作了一个表单,可以在带有JavaScript的HTML文本区域中显示结果。 I'm trying to make the textarea also display a link to the wikipedia article about the item selected using an if/else statement: 我正在尝试使文本区域还显示到有关使用if / else语句选择的项目的Wikipedia文章的链接:

         function setFlower(type) {
        flowerName = type;  
    }

    //method for displaying the method in the textarea
    function displayMessage() {
        var fullName = document.flowerOrderForm.fullName.value;

        // if/else statements for more information           
        if (flowerName == document.flowerOrderForm.flowerTypes.roses) {
            var moreInfo = "https://en.wikipedia.org/wiki/Rose";
        } 
        else if (flowerName == document.flowerOrderForm.flowerTypes.carnations) {
                var moreInfo = "https://en.wikipedia.org/wiki/Dianthus_caryophyllus";
            } 

        else if (flowerName == document.flowerOrderForm.flowerTypes.daisies) {
                var moreInfo = "https://en.wikipedia.org/wiki/Asteraceae";
            }           


        document.flowerOrderForm.info.value = 
        fullName + ", " + "thank you for your inquiry about " + flowerName + "." + NL
        + "Here is a link for more information: " + moreInfo;
    }

And the HTML form: 和HTML表单:

        <form name = "flowerOrderForm">
        <fieldset name = "form"> 
            <fieldset name = "inputControls"> 
                <p class = "name">
                    <!--Name textbox and label-->
                    <label for = "fullName">Full Name</label><br />
                    <input class = "fullName" type = "text" name = "fullName" value = "" id = "fullName" size = "35" />
                </p>
                <p class = "flowers">
                        <!--flower type radio buttons-->
                    <span>
                        <input type = "radio" name = "flowerTypes" value = "roses" id = "roses" onclick = "setFlower(this.value)" />
                        <label for= "roses">Roses</label>  
                    </span>
                    <span>   
                        <input type = "radio" name = "flowerTypes" value = "carnations" id = "carnation" onclick = "setFlower(this.value)" />
                        <label for = "carnation">Carnations</label>
                    </span>
                    <span>
                        <input type = "radio" name = "flowerTypes" value = "daisies" id = "daisy" onclick = "setFlower(this.value)" />
                        <label for = "daisy">Daisies</label>
                    </span>
                </p><!--end flowers-->
            </fieldset><!--end inputControls-->

            <fieldset name = "submit">
                <!--request info submit button-->
                <input class = "requestInfo" type = "button" name = "flowerOrder" value = "Request Information" onclick = "displayMessage()" />
            </fieldset><!--end submit-->
            <fieldset name = "infoBox">
                <!--textarea for displaying submitted information-->
                <textarea name = "info" readonly = "true" value = "" rows = "7" cols = "50"></textarea>
            </fieldset><!--end infoBox-->
        </fieldset><!--end form-->
    </form>

Right now, moreInfo is undefined in the text area. 现在,在文本区域中未定义moreInfo。 How can I fix this? 我怎样才能解决这个问题?

Dont use document.write all it does it print it out to the screen instead store it as link 不要使用document.write所有它会打印到屏幕上,而是将其存储为链接

         function setFlower(type) {
    flowerName = type;  
}

//method for displaying the method in the textarea
function displayMessage() {
    var fullName = document.flowerOrderForm.fullName.value;
    var moreInfo;

    // if/else statements for more information           
    if (flowerName == document.flowerOrderForm.flowerTypes.roses) {
        moreInfo = "https://en.wikipedia.org/wiki/Rose";
    } 
    else if (flowerName == document.flowerOrderForm.flowerTypes.carnations) {
            moreInfo = "https://en.wikipedia.org/wiki/Dianthus_caryophyllus";
        } 

    else if (flowerName == document.flowerOrderForm.flowerTypes.daisies) {
            moreInfo = "https://en.wikipedia.org/wiki/Asteraceae";
        }



    document.flowerOrderForm.info.value = 
    fullName + ", " + "thank you for your inquiry about " + flowerName + "." + NL
    + "Here is a link for more information: " + moreInfo; 
    //  moreInfo would be a link now as a string and so will be displayed in textarea
}

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

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