javascript - 如何获取输入填充的值
问题描述
我想将输入填充的值从一个函数获取到另一个函数,但我不确定如何。
从此函数输入填充:
$("#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");
}
});
解决方案
将 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");
}
});
推荐阅读
- javascript - 如何使用 ajax 和 ac# 方法填充下拉列表
- tensorflow - tensorflow 如何在 Tensorflow Tensor 中获取唯一值的索引?
- netsuite - 在 Netsuite 中通过 PHP 创建采购订单时更改项目价格
- objective-c - 通用类 Lottie swift
- mysql - php artisan 迁移显示错误
- r - 如何找到序列矩阵中两点之间的距离?
- ruby - Thread.critical 在 ruby 2.5.0 中不起作用
- javascript - 如何将应用程序从浏览器重定向到 Safari?
- c# - Amazon S3 SDK 不允许我使用 GetObjectMetadata。它只允许我使用 GetObjectMetadataAsync
- flutter - Flutter 中的多页面状态处理