Concatenate multiple HTML text inputs with stored variable

后端 未结 2 841
忘了有多久
忘了有多久 2020-12-20 06:52

I am trying to create a simple html form which asks for some details from a user and once this has been submitted will add the text to some predetermined text in a text box.

2条回答
  •  借酒劲吻你
    2020-12-20 07:41

    Here is a solution, so get the elements using document.getElementById(), attach an event handler to the click event using element.onclick = function () {} and alert() to show a message box.

    jsFiddle

    JavaScript

    var button = document.getElementById('test');
    var name = document.getElementById('name');
    var age = document.getElementById('age');
    var location = document.getElementById('location');
    
    button.onclick = function () {
        var str = 'Hello ' + name.value + 
            ', you are ' + age.value +
            ' years old and from ' + location.value;
        alert(str);
    };
    

    HTML

    
    


    Edit

    To output it to the page use element.innerHTML to set the contents of an element.

    jsFiddle

    output.innerHTML = str;
    

提交回复
热议问题