我有一个GET请求,它从服务器端函数获取一些信息,并将数据附加到一系列div。但是,当我在console.log中回复时,我给出了网站的整个HTML,而不仅仅是我需要的值。
请求
null
CONSOLE.LOG
$('#verify').click(function() {
var parameters = {
firstName: $('#firstName').val(),
lastName: $('#lastName').val(),
email: $('#email').val() };
$.get('/verify', parameters, function(data) {
console.log(data); //Returns full body of HTML
$('#firstLast').val(data); // Doesn't work
});
});
在console.log里面是我需要的:
<!DOCTYPE html>
<html>
<head></head>
<title>
Demo
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="Demo project">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<style type="text/css"></style>
</title>
<body>
<form method="post" action="post" id="plans" class="plans"><input id="firstName" type="text" name="firstName" value="rhys"><input id="lastName" type="text" name="lastName" value="edwards"><input id="email" type="text" name="email" value="@rhysedwards.com"><input id="submit" type="submit" name="submit" value="submit"></form>
<div id="firstLast">rhysedwards@rhysedwards.com: false</div>
<div id="firstDotLast">rhys.edwards@rhysedwards.com: false</div>
<div id="fInitialLastName">redwards@rhysedwards.com: false</div>
<div id="fInitialDotLastName">r.edwards@rhysedwards.com: false</div>
<div id="firstNameLInitial">rhyse@rhysedwards.com: false</div>
<div id="firstNameDotLInitial">rhys.e@rhysedwards.com: false</div>
<div id="firstNameOnly">rhys@rhysedwards.com: true</div>
<div id="lastNameOnly">edwards@rhysedwards.com: false</div>
<div id="firstNameUnderscoreLastName">rhys_edwards@rhysedwards.com: false</div>
<div id="fInitialUnderscoreLastName">r_edwards@rhysedwards.com: false</div>
<div id="firstNameUnderscoreLInitial">rhys_e@rhysedwards.com: false</div>
<input id="verify" name="verify" value="verify" type="submit"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script><script src="./javascripts/script.js"></script>
</body>
</html>
玉模板:
<div id="firstLast">rhysedwards@rhysedwards.com: false</div>
<div id="firstDotLast">rhys.edwards@rhysedwards.com: false</div>
<div id="fInitialLastName">redwards@rhysedwards.com: false</div>
<div id="fInitialDotLastName">r.edwards@rhysedwards.com: false</div>
<div id="firstNameLInitial">rhyse@rhysedwards.com: false</div>
<div id="firstNameDotLInitial">rhys.e@rhysedwards.com: false</div>
<div id="firstNameOnly">rhys@rhysedwards.com: true</div>
<div id="lastNameOnly">edwards@rhysedwards.com: false</div>
<div id="firstNameUnderscoreLastName">rhys_edwards@rhysedwards.com: false</div>
<div id="fInitialUnderscoreLastName">r_edwards@rhysedwards.com: false</div>
<div id="firstNameUnderscoreLInitial">rhys_e@rhysedwards.com: false</div>
如何从block content
form(method='post', action='post', class='plans', id='plans')
input#firstName(type='text', name='firstName' value="rhys")
input#lastName(type='text', name='lastName' value="edwards")
input#email(type='text', name='email' value="@rhysedwards.com")
input#submit(type='submit', name='submit', value='submit')
#firstLast !{resultA}
#firstDotLast !{resultB}
#fInitialLastName !{resultC}
#fInitialDotLastName !{resultD}
#firstNameLInitial !{resultE}
#firstNameDotLInitial !{resultF}
#firstNameOnly !{resultG}
#lastNameOnly !{resultH}
#firstNameUnderscoreLastName !{resultI}
#fInitialUnderscoreLastName !{resultJ}
#firstNameUnderscoreLInitial !{resultK}
input#verify(name='verify', value='verify', type='submit')
参数中获取每个值的div值?
答案 0 :(得分:1)
由于您要将数据放入div
元素,请使用$('#firstLast').html(data);
.val() //is used for input elements