根据单击的按钮获取表单内的输入值

时间:2021-10-01 21:24:59

I've got the following form:

我有以下表格:

<div>
  <form name="ajaxformname" id="ajaxform" action="/request" method="POST">
    AJAX:
    <input id="btn1" name="feeling" type="submit" value="Fine">
    <input id="btn2" name="feeling" type="submit" value="Neutral">
    <input id="btn3" name="feeling" type="submit" value="Bad">
  </form>
</div>

which should be posted to a server via ajax.

应该通过ajax发布到服务器。

Here is the associated javascript:

这是相关的javascript:

$('#ajaxform').submit(function (event) {
    event.preventDefault();
        var form = $(this); 
        var action = form.attr("action"), 
            method = form.attr("method"),
            data = form.attr("value");

    $.ajax({
        url: "/request",
        type: method,
        data: data
    });

Now - depending on which of the three buttons has been clicked - I want their values to be posted to the server. But form.attr("value") just gives me the value of the form but not of the input field.

现在 - 根据点击的三个按钮中的哪一个 - 我希望将它们的值发布到服务器。但是form.attr(“value”)只给出了表单的值而不是输入字段的值。

Any suggestions? A solution would be to create the different forms but that doesn't seems to be DRY ...

有什么建议?一个解决方案是创建不同的形式,但似乎并不干......

2 个解决方案

#1


2  

First thing, if you wanna use the action attribute of the form, you should reference it in the url param of ajax request:

首先,如果你想使用表单的action属性,你应该在ajax请求的url参数中引用它:

url: form.attr('action'),

Now, for the button clicked, you should use this (it's a workaround because submit buttons are not incluided in the form serialization, if not, I would use it instead):

现在,对于单击的按钮,你应该使用它(这是一种解决方法,因为提交按钮不包含在表单序列化中,如果没有,我会改为使用它):

$(function () {

    //When any of the buttons is clicked, we store in the form data the clicked button value
    $('#ajaxform').on('click', 'input[type=submit][name=feeling]', function(e) {

        $(this.form).data('clicked', this.value);
    });

    $('#ajaxform').submit(function (event) {

        event.preventDefault();

        var form = $(this);

        $.ajax({
            url: form.attr('action'),
            type: form.attr("method"),
            data: { clickedButton : form.data('clicked') } //Retrieve the button clicked value from the form data
        });
    });
});

Here the working example: https://jsfiddle.net/68qLxLgm/1/

这里是工作示例:https://jsfiddle.net/68qLxLgm/1/

#2


0  

Hi kindly use the following to get the id of the button that has been clicked

嗨请使用以下内容来获取已单击的按钮的ID

$("input").click(function(e){
    var idClicked = e.target.id;
});

#1


2  

First thing, if you wanna use the action attribute of the form, you should reference it in the url param of ajax request:

首先,如果你想使用表单的action属性,你应该在ajax请求的url参数中引用它:

url: form.attr('action'),

Now, for the button clicked, you should use this (it's a workaround because submit buttons are not incluided in the form serialization, if not, I would use it instead):

现在,对于单击的按钮,你应该使用它(这是一种解决方法,因为提交按钮不包含在表单序列化中,如果没有,我会改为使用它):

$(function () {

    //When any of the buttons is clicked, we store in the form data the clicked button value
    $('#ajaxform').on('click', 'input[type=submit][name=feeling]', function(e) {

        $(this.form).data('clicked', this.value);
    });

    $('#ajaxform').submit(function (event) {

        event.preventDefault();

        var form = $(this);

        $.ajax({
            url: form.attr('action'),
            type: form.attr("method"),
            data: { clickedButton : form.data('clicked') } //Retrieve the button clicked value from the form data
        });
    });
});

Here the working example: https://jsfiddle.net/68qLxLgm/1/

这里是工作示例:https://jsfiddle.net/68qLxLgm/1/

#2


0  

Hi kindly use the following to get the id of the button that has been clicked

嗨请使用以下内容来获取已单击的按钮的ID

$("input").click(function(e){
    var idClicked = e.target.id;
});