var newusername=$(this).val();仅发送按钮的值


var newusername=$(this).val(); only sends out the value of the button

这是我的问题,当我尝试将我的数据发送到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);
    });
}

我看到两件事:

  • 发送的对象的定义很糟糕。该对象由键值对组成,您可以将这些键值对设置为硬编码的字符串值。

  • 开始时$(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);
        });
}

假设这是针对表单的,您可以使用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);

最好绑定到提交事件,这样用户就不会使用键盘绕过单击。

看起来你可能有一个关于事件处理程序如何在jQuery中工作的概念错误。

通过调用$("#btn-reg").on("click", reg);

您告诉 jQuery 在按下#btn-reg按钮时调用该方法reg。发生这种情况时,jQuery将在传递按下的元素的同时调用reg。(这是为了支持多个事件处理程序调用一个方法,例如,如果您有 2 个按钮)。

因此,通过使用 $(this) ,您实际上是在调用 $("#btn-reg")

因此$(this).val();会给你<button>的值,你把它分配给三个变量newusernamenewpasswordnewclub。 - 这就解释了为什么你在PHP脚本上看到<button>的值。


虽然您没有提供表单的 HTML 内容,但下面是我如何使用通过 javascript 提交的简单 3 字段表单的示例:

.HTML:

<input type="text" id="newusername" />
<input type="text" id="newpassword" />
<input type="text" id="newclub" />
<button id="btn-reg">Submit</button>

JavaScript:

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);
    });
}

不能将这些变量作为参数传递。此外,您正在将字符串传递到后端,这是无法正常工作的。此外,您不是选择输入字段的值,而是选择按钮本身的值。

.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);
        }
    });
}
var newusername="newusername"; // or get your needed value anywhere else
var newpassword="newpassword"; // or get your needed value anywhere else
var newclub="newclub"; // or get your needed value anywhere else
$("#btn-reg").on("click", function () {
$.post('classCalling.php', {
    newusername: newusername,
    newpassword: newpassword,
    newclub: newclub
},
function(data){
    console.log(data);
});
});

它会帮助你。