简体   繁体   English

如何获取提交表单的输入值

[英]how to get the input value of a submitted form

I have the bellow 'html' code: 我有下面的“ html”代码:

<div class="userbox" id="'.$data[0]->id.'">
    <form action="schedule.php?page=management&ajax=true" method="post">
        <input type="hidden" name="id" value="'. $data[0]->id .'" class="id">
        <input type="submit" name="remove_entry" value="x" class="ajax_btn">
    </form>
</div>

And the bellow JS code: 和下面的JS代码:

$(document).ready(
function () {

    $('.userbox form').on('submit',  function(ev) {

        ev.preventDefault();


        var idVal = $('.id').val();

        alert(idVal);

        $.post("schedule.php?page=management&ajax=true", { id : idVal }, function (data) {
            $("#" + idVal + ".userbox").remove();
            $("#" + idVal + ".user_data").css('background-color', '#FFFFFF');

        }
    );

    });

});

I'm trying to get the value of the id from the form (taking in consideration there are multiple forms on page) but it's grabbing the first id on page instead of the one from the submitted form. 我正在尝试从表单获取ID的值(考虑到页面上有多个表单),但是它正在获取页面上的第一个ID,而不是提交表单中的ID。 Do you guys have any suggestions ? 你们有什么建议吗?

The query selector you use ($('.id')) is searching the whole document for nodes with the class "id" and is not limited to the submitted form. 您使用的查询选择器($('。id'))在整个文档中搜索具有“ id”类的节点,而不仅限于提交的表单。

Try to define your scope and search inside like this: 尝试定义您的范围并在内部进行搜索,如下所示:

$('.userbox form').on('submit',  function(ev) {
    var scope = $(this);

    var idVal = scope.find('.id').val();
    // ....
}

您可以按名称选择输入

var idVal = $(this).find( "input[name*='id']" ).val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM