我无法序列化表单的数据。代码如下。控制台只记录空白。
HTML:
<div id="sm-log-in">
<form>
<div class="row">
<div class="small-12 medium-4 small-centered columns">
<div class="row">
<div class="medium-2 columns">
<label for="username" class="inline">Username:</label>
</div>
<div class="medium-10 columns">
<input type="text" id="username" placeholder="Username">
<!--<small class="error">A username is required.</small>-->
</div>
</div>
<div class="row">
<div class="medium-2 columns">
<label for="password" class="inline">Password:</label>
</div>
<div class="medium-10 columns">
<input type="password" id="password" placeholder="Password">
<!--<small class="error">A password is required.</small>-->
</div>
</div>
<div class="row">
<div class="columns">
<input id="remember-me" type="checkbox" class="right"><label for="remember-me" class="right">Remember Me</label>
</div>
</div>
<div class="row">
<div class="columns">
<button type="submit" class="small right">Log In</button>
</div>
</div>
</div>
</div>
</form>
JS:
$('#sm-log-in form').submit(function(e){
e.preventDefault();
console.log($( this ).serialize());
//etc
答案 0 :(得分:1)
表单字段需要具有name
属性。这是键值的方式。 serialize
方法将每个输入的名称与其值配对。由于它们都没有任何名称,因此不包括任何名称。
答案 1 :(得分:0)
请为每个输入元素添加“name”属性,如下所示:
<input type="text" id="username" name="username" placeholder="Username">
<input type="password" id="password" name="password" placeholder="Password">