$(document).ready(function() {
$("#register button[name=btnPosta]").click(function() {
console.log('clicked');
thisBtn = $(this);
parent = $(this).parent();
name = parent.data('name');
$(this).attr('disabled', true);
$.post('register.php', {
name: (name)
}, function(data) {
console.log('Ajax success');
$('#message').html(data);
thisBtn.attr('disabled', false); // reset
});
console.log('Ajax success');
});
});
});
这是我的JavaScript函数,它发送name的值(从输入框输入),但显示未定义而不是输入框中输入的内容。
<div class = "Interactlogin" id = "register">
<table>
<tr>
<td> Username: <td>
<td> <input type = "text" class = "field" name = "name"> </td>
</tr>
<tr>
<td> Password: <td>
<td> <input type = "password" class = "field" name = "password"> </td>
</tr>
<td > Email: <td>
<td> <input type = "email" class = "field" name = "email"> </td>
</tr>
<tr>
<td> <td>
<td><button class='regular' id = "btnPost" name = 'btnPosta'> Log In </button <td>
</tr>
<tr>
</table>
</div>
<div id = "message">
</div>
这是输入所在的HTML标记。
答案 0 :(得分:1)
你正试图使用$ .data而不是$ .val你可能意味着。
使用
name = $('#register input[name="name"]').val();
而不是
name = parent.data('name');
答案 1 :(得分:0)
而不是
name = parent.data('name');
使用:
name = $('#register').find('input[name="name"]').val();
答案 2 :(得分:0)
请改为尝试:
$(document).ready(function() {
$('#register :button[name=btnPosta]').click(function() {
console.log('clicked');
var name = $('#register :input[name = "name"]').val();
$(this).attr('disabled', true);
$.post('register.php',
name: name,
function(data) {
console.log('Ajax success');
$('#message').html(data);
$(this).attr('disabled', false);
});