gpt4 book ai didi

javascript - form.serialize() 不起作用

转载 作者:行者123 更新时间:2023-12-02 14:21:36 25 4
gpt4 key购买 nike

HTML 代码

<div id="address" class="col s12">
<div class="row">
<form method="post" action="" id="addressDetails">
<div class="input-field col s6">
<textarea id="lAddress" name = 'lAddress' minlength='20' maxlength='100' class="materialize-textarea" class="validate" required length="100"></textarea>
<label for="lAddress" data-error="Must be between 20 to 100 Characters">Local Address</label>
</div>
<div class="input-field col s6">
<textarea id="pAddress" name = 'pAddress' minlength='20' maxlength='100' class="materialize-textarea" class="validate" required length="100"></textarea>
<label for="pAddress" data-error="Must be between 20 to 100 Characters">Permanent Address</label>
</div>
</form>
</div>
<div class="row center-align">
<button type="submit" name="submitAddress" form="addressDetails" class="waves-effect waves-light light-blue darken-1 btn updateProfile">Save Address Details</button>
</div>
</div>

JS代码

function updateProfile(event) {
console.log(this);
event.preventDefault();
form = $(this).closest('.col s12').find('form');
console.log($(form));
$.ajax('profile/updateProfile.php', {
type: "POST",
dataType: "json",
data: form.serialize(),
success: function(result) {
//console.log(result);
}
});
}

$(document).ready(function() {
$("button.updateProfile").on('click', updateProfile);
});

我使用 Chrome 调试器检查网络 AJAX 调用。没有传递任何键值对。

控制台日志中没有错误。

最佳答案

closest() 中的选择器不正确。不应该有空格,并且两个值前面都应该有 . 来表示类选择器。试试这个:

var form = $(this).closest('.col.s12').find('form');

Working example

关于javascript - form.serialize() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38612741/

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