Javascript var newusername=$(this.val();仅发送按钮的值
这是我的问题,当我试图将数据发送到PHP时,它只发送Javascript var newusername=$(this.val();仅发送按钮的值,javascript,php,jquery,Javascript,Php,Jquery,这是我的问题,当我试图将数据发送到PHP时,它只发送或的值 如果我删除变量的定义,它只会将数据作为字符串发送,如果我有这样的变量 newusername: "newusername", newpassword: "newpassword", newclub: "newclub" 如果我删除”,我将在jquery.mini.js中得到一个错误 HTML: 我看到两件事: 发送的对象的定义不正确。该对象由键-值对组成,您可以将其设置为硬编码的字符串值 开头的变量捕获$(this).val()都是
或
的值
如果我删除变量的定义,它只会将数据作为字符串发送,如果我有这样的变量
newusername: "newusername",
newpassword: "newpassword",
newclub: "newclub"
如果我删除”
,我将在jquery.mini.js中得到一个错误
HTML:
我看到两件事:
- 发送的对象的定义不正确。该对象由键-值对组成,您可以将其设置为硬编码的字符串值
- 开头的变量捕获
都是相同的。您没有将其作为DOM对象获取。您应该在括号中进行CSS查询(即,$(this).val()
)$(“--this--”
我看到两件事:
- 发送的对象的定义不正确。该对象由键-值对组成,您可以将其设置为硬编码的字符串值
- 开头的变量捕获
都是相同的。您没有将其作为DOM对象获取。您应该在括号中进行CSS查询(即,$(this).val()
)$(“--this--”
不能将这些变量作为参数传递。此外,您正在向后端传递字符串,这是无法工作的。此外,您选择的不是输入字段的值,而是按钮本身的值 HTML:
不能将这些变量作为参数传递。此外,您正在向后端传递字符串,这是无法工作的。此外,您选择的不是输入字段的值,而是按钮本身的值 HTML: 它会帮助你的
这将对您有所帮助。在jQuery中事件处理程序的工作方式方面,您可能存在概念上的错误 通过在(“点击”,注册)上调用
$(“#btn注册”)代码>,
您告诉jQuery在按下#btn reg
按钮时调用方法reg
。发生这种情况时,jQuery将在传递按下的元素时调用reg
。(这是为了支持多个事件处理程序调用一个方法,例如,如果您有两个按钮)
因此,通过使用$(this)
,您实际上是在调用$(“#btn reg”)
$(this.val()因此,code>将为您提供
的值,您将其分配给三个变量newusername
、newpassword
和newclub
-这解释了为什么您在PHP脚本上看到
的值
虽然您尚未提供表单的HTML内容,但下面是我如何使用通过javascript提交的简单3字段表单的示例:
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);
});
}
HTML:
提交
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);
}
});
}
console.log('Script loaded...');
$("#btn-reg").on("click", reg);
function reg(e) {
e.preventDefault();
console.log('Klick, klick...');
// You are declaring new variables here
var newusername = $("#newusername").val();
var newpassword = $("#newpassword").val();
var newclub = $("#newclub").val();
// And using them here. The format used here is
// <name to submit>: <value to submit>
// That is why the variable names should not be quoted on the right
$.post('classCalling.php', {
newusername: newusername,
newpassword: newpassword,
newclub: newclub
},
function(data){
console.log(data);
});
}
console.log('Script-loaded…');
美元(“点击”,注册);
功能注册(e){
e、 预防默认值();
log('Klick,Klick…');
//您正在这里声明新变量
var newusername=$(“#newusername”).val();
var newpassword=$(“#newpassword”).val();
var newclub=$(“#newclub”).val();
//在这里使用它们。这里使用的格式是
// :
//这就是为什么不应该在右边引用变量名的原因
$.post('classCalling.php'{
newusername:newusername,
newpassword:newpassword,
新俱乐部:新俱乐部
},
功能(数据){
控制台日志(数据);
});
}
在jQuery中事件处理程序的工作方式方面,您可能存在概念性错误
通过在(“点击”,注册)上调用$(“#btn注册”)代码>,
您告诉jQuery在按下#btn reg
按钮时调用方法reg
。发生这种情况时,jQuery将在传递按下的元素时调用reg
。(这是为了支持多个事件处理程序调用一个方法,例如,如果您有两个按钮)
因此,通过使用$(this)
,您实际上是在调用$(“#btn reg”)
$(this.val()因此,code>将为您提供
的值,您将其分配给三个变量newusername
、newpassword
和newclub
-这解释了为什么您在PHP脚本上看到
的值
虽然您尚未提供表单的HTML内容,但下面是我如何使用通过javascript提交的简单3字段表单的示例:
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);
});
}
HTML:
提交
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);
}
});
}
console.log('Script loaded...');
$("#btn-reg").on("click", reg);
function reg(e) {
e.preventDefault();
console.log('Klick, klick...');
// You are declaring new variables here
var newusername = $("#newusername").val();
var newpassword = $("#newpassword").val();
var newclub = $("#newclub").val();
// And using them here. The format used here is
// <name to submit>: <value to submit>
// That is why the variable names should not be quoted on the right
$.post('classCalling.php', {
newusername: newusername,
newpassword: newpassword,
newclub: newclub
},
function(data){
console.log(data);
});
}
console.log('Script-loaded…');
美元(“点击”,注册);
功能注册(e){
e、 预防默认值();
log('Klick,Klick…');
//您正在这里声明新变量
var newusername=$(“#newusername”).val();
var newpassword=$(“#newpassword”).val();
var newclub=$(“#newclub”).val();
//在这里使用它们。这里使用的格式是
// :
//这就是为什么不应该在右边引用变量名的原因
$.post('classCalling.php'{
newusername:newusername,
newpassword:newpassword,
新俱乐部:新俱乐部
},
功能(数据){
控制台日志(数据);
});
}
假设这是一个表单,您可以使用该表单发送与输入控件名称相同的键来简化数据
<form id="password-form">
<input name="newusername">
<input name="newpassword">
<select name="newclub"></select>
<input type="submit" value="Submit">
</form>
或使用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);
最好是绑定到提交事件,这样用户就不会绕过使用键盘的单击操作,假设这是一个表单,您可以使用它来简化数据,从而发送与输入控件名称相同的键
<form id="password-form">
<input name="newusername">
<input name="newpassword">
<select name="newclub"></select>
<input type="submit" value="Submit">
</form>
或使用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);
最好是绑定到提交事件,这样用户就不会使用键盘绕过单击,而将newusername
、newpassword
和newclub
全部分配到您单击的按钮的“值”。你怎么办
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);