首页 > 解决方案 > 如何获取输入填充的值

问题描述

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

从此函数输入填充:

$("#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");
                    }
                });

标签: javascripthtml

解决方案


将 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");
                }
            });

推荐阅读