如何获得输入填充的值

时间:2019-10-16 01:27:17

标签: javascript html

我想从一个函数到另一个函数获取输入填充的值,但是我不确定如何。

通过此功能输入填充:

$("#buttonDone4").click(function () {
                    function productAddToTable() {
                        $("#table").fadeIn();
//                     First check if a <tbody> tag exists, add one if not
                        if ($("#table tbody").length == 0) {
                            $("#table").after("<tbody></tbody>");
                        }
                        // Append product to the table
                        $("#table").append(
                                "<tr>" +
                                "<td>" + document.getElementById("perimeter2").innerHTML + "</td>" +
                                "<td>" + "4" + "</td>" +
                                "<td>" + "<input type='text' name='periAns' size='10'/>" + "</td>" +
                                "</tr>"
                                );
                    }

                    productAddToTable();
                    $("#buttonDone4").fadeOut();
                    $(".p1").fadeIn();
                    $("#buttonCheck").fadeIn();
                });

此功能:

 $("#buttonCheck").click(function () {
                var pa = document.getElementByName["periAns"].value;
                var peri = (document.getElementById("perimeter2").innerHTML / 4);
                    if(peri == pa ){
                        console.log("yes");
                    }else{
                        console.log("no");
                    }
                });

1 个答案:

答案 0 :(得分:2)

将ID放入输入并使用jquery。

$("#table").append(
         "<tr>" +
         "<td>" + document.getElementById("perimeter2").innerHTML + "</td>" +
         "<td>" + "4" + "</td>" +
         "<td>" + "<input type='text' id ='periAns' size='10'/>" + "</td>" +
         "</tr>"
 );
                }

$("#buttonCheck").click(function () {
            var pa = Number($("#periAns").val()) //assuming this is an input
            var peri = Number(document.getElementById("perimeter2").innerHTML)
            var finPeri = peri / 4

                if(finPeri == pa ){
                    console.log("yes");
                }else{
                    console.log("no");
                }
            });