How to pass a form input value into a JavaScript function

后端 未结 6 1368
野的像风
野的像风 2020-12-24 08:01

I have a generic JavaScript function which takes one parameter

function foo(val) { ...} 

and I want to call the function when submit a form

6条回答
  •  暖寄归人
    2020-12-24 08:40

    Give your inputs names it will make it easier

    UPDATE:

    If you give your button an id things can be even easier:

    Javascript:

    var button = document.getElementById("theButton"),
    value =  button.form.valueId.value;
    button.onclick = function() {
        foo(value);
    }
    

提交回复
热议问题