这就是我的问题,当我试图发送我的数据到一个PHP时,它只发送<button>或<input type="button">的值。
如果我删除了变量的定义,它只会将数据作为字符串发送,如果我有这样的数据
newusername: "newusername",
newpassword: "newpassword",
newclub: "newclub"如果我删除"",我会在jquery.mini.js中得到一个错误
HTML:
<form method="post">
<div class="form-group">
<label> Username </label>
<input type="text" class="form-control" id="newusername">
</div>
<div class="form-group">
<label> Password </label>
<input type="password" class="form-control" id="newpassword">
</div>
<div class="form-group">
<label> Your club </label>
<input type="text" class="form-control" id="newclub">
</div>
<input type="button" id="btn-reg">
</form>Javascript:
console.log('Script loaded...');
$("#btn-reg").on("click", reg);
function reg(newusername, newpassword, newclub) {
console.log('Klick, klick...');
var newusername = $(this).val();
var newpassword = $(this).val();
var newclub = $(this).val();
$.post('classCalling.php', {
newusername: "newusername",
newpassword: "newpassword",
newclub: "newclub"
},
function(data){
console.log(data);
});
}发布于 2016-03-02 00:57:42
我看到了两件事:
$(this).val()开头的变量捕获将完全相同。您没有将其作为DOM对象获取。您应该在括号中执行CSS查询(即$("---this--")).function reg(newusername, newpassword, newclub) {
console.log('Klick, klick...');
// if there is a input with class newusernameand only one
var newusername = $('input.newusername').val();
//if there is a input with class newpassword and only one
var newpassword = $('input.newpassword').val();
//if there is a input with class newclub and only one
var newclub = $('input.newclub').val();
$.post('classCalling.php',
{
newusername: newusername,
newpassword: newpassword,
newclub: newclub
},
function(data) {
console.log(data);
});
}发布于 2016-03-02 01:10:10
假设这是一个表单,您可以通过使用serialize()来简化数据,它将发送与输入控件名称相同的键
<form id="password-form">
<input name="newusername">
<input name="newpassword">
<select name="newclub"></select>
<input type="submit" value="Submit">
</form>JS
$('#password-form').submit(function(event){
event.preventDefault(); // stop default from reloading page
var postData = $(this).serialize();
$.post('classCalling.php', postData , function(response){
console.log(response);
});
});或者使用reg()作为事件处理函数来编写:
function reg(event){
event.preventDefault(); // stop default from reloading page
var postData = $(this).serialize();
$.post('classCalling.php', postData , function(response){
console.log(response);
});
}
$('#password-form').submit(reg);最好是绑定到提交事件,这样用户就不会使用键盘绕过点击
发布于 2016-03-02 00:58:11
您不能将这些变量作为参数传递。此外,你正在向后端传递字符串,这是不能工作的。此外,您选择的不是输入字段的值,而是按钮本身的值。
HTML:
<form>
<input type="text" class="newusername" />
<input type="password" class="newpassword" />
<input type="text" class="newclub" />
<button class="btn-reg">Submit</button>
</form>JavaScript:
console.log('Script loaded...');
$(".btn-reg").on("click", reg);
function reg(event) {
console.log('Klick, klick...');
event.preventDefault(); // prevents the form to be submitted
var newusername = $(".newusername").val();
var newpassword = $(".newpassword").val();
var newclub = $(".newclub").val();
$.post('classCalling.php', {
newusername: newusername,
newpassword: newpassword,
newclub: newclub
},
function(data) {
console.log(data);
}
});
}https://stackoverflow.com/questions/35728699
复制相似问题