gpt4 book ai didi

javascript - 在同一页面显示表单输入

转载 作者:行者123 更新时间:2023-11-30 18:17:24 25 4
gpt4 key购买 nike

我正在制作一个简单的表单演示,我希望输入显示在表单下方。目前我在控制台中填充它。当我单击提交按钮时,它如何显示在 div 中?我的代码:

$(document).ready(function() {
$('.submitForm').on('click',function(event){
event.preventDefault();

$('#firstName').val();
$('#lastName').val();
$('#phoneNumber').val();
$('#address').val();

console.log($('#firstName').val());
console.log($('#lastName').val());
console.log($('#phoneNumber').val());
console.log($('#address').val());
});
});

最佳答案

好吧,您目前没有将这些值放在任何地方,而是放在 console.log 中。

我希望看到类似的东西(让我们称你想要值去的 div,“输出”):

$(document).ready(function() {
$('.submitForm').on('click',function(event){
event.preventDefault();

// Borrowing from another response, this is better
// Putting these in variables protects you from
// 1) accidentally modifying your form values
// 2) invalid input, if you add some basic checks, like
// testing to see if the length is > 0, doesn't contain
// bad characters, etc.
var firstName = $('#firstName').val(),
lastName = $('#lastName').val(),
phone = $('#phoneNumber').val(),
address = $('#address').val();

// get a reference to the div you want to populate
var $out = $("#output");

// This is a better way of dealing with this
// because every call to .append() forces DOM
// reparsing, and if you do this too often, it can cause
// browser slowness. Better to put together one string
// and add it all at once.
$out.html("<p>" + firstName + "</p>" +
"<p>" + $('#lastName').val() + "</p>" +
"<p>" + $('#phoneNumber').val() + "</p>" +
"<p>" + $('#address').val() + "</p>");
});
});

关于javascript - 在同一页面显示表单输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12938095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com