简体   繁体   English

jQuery基于按钮单击更改表单操作

[英]jQuery change form action based on button click

I am trying to change the form action in my jade file based on the button click, "Save" and "Delete". 我正在尝试基于按钮“保存”和“删除”来更改玉器文件中的表单动作。

form(method='post', id='updateForm')
        label Course Number
        input(type='text', name='courseNum', value= courses[0].courseNum)
        br
        label Course Name
        input(type='text', name='courseName', value= courses[0].courseName
        br
        button#btnSave(type='button') SAVE
        button#btnDelete(type='button') DELETE

script.
    $('#btnDelete').click(function(){
        var action = $(this).val() == '/course/delete/'+courses[0].courseId;
        $('#updateForm').attr('action', action);
        $('#updateForm').submit();
    });
    $('#btnSave').click(function(){
        var action = $(this).val() == '/course/update/' + courses[0].courseId;
        $('#updateForm').attr('action', action);
        $('#updateForm').submit();
    });

The input value (courses item) is passed correctly so I assume that in the script it is also passed correctly. 输入值(课程项目)正确传递,因此我认为在脚本中它也正确传递。 But, why the form is not submitting? 但是,为什么不提交表单? There are no actions shown. 没有显示任何操作。

Does my script is not correct? 我的脚本不正确吗? What is the best way to change form action? 更改表单动作的最佳方法是什么?

Thanks. 谢谢。

OK try the following: 确定,请尝试以下操作:

var submitForm = function(method){
    //set form action based on the method passed in the click handler, update/delete
    var formAction = '/course/' + method + '/' + courses[0].courseId;
    //set form action
    $('#updateForm').attr('action', formAction);
    //submit form
    $('#updateForm').submit();
};

$(document)
.on('click', '#btnDelete', function(){
    //set your method - delete
    submitForm('delete');
})
.on('click', '#btnSave', function(){
    //set your method - update
    submitForm('update');
});

I think your error is on this line: 我认为您的错误在这条线上:

var action = $(this).val() == '/course/delete/'+courses[0].courseId;

Here the first equals (=) will assign the part on the right to the variable action. 在这里,第一个等号(=)将把右边的部分分配给可变操作。 All good there. 那里一切都很好。 The double equals (==) however is testing whether $(this).val() is equal to the string on the right ('/course/delete/'+courses[0].courseId;). 但是, double equals(==)正在测试$(this).val() 是否等于右侧的字符串('/course/delete/'+courses[0].courseId;)。

It seems you are assigning a boolean to action. 看来您是要为操作分配布尔值。 If you want to concatenate the string you need something like this: 如果要连接字符串,则需要这样的操作:

var action = $(this).val() + '/course/delete/' + courses[0].courseId;

If that's what you are trying to do. 如果这就是您要尝试做的。 If not then post an error message and we can help more. 如果没有,请发布错误消息,我们可以提供更多帮助。

form(method='post', id='updateForm')
        label Course Number
        input(type='text', name='courseNum', value= courses[0].courseNum)
        br
        label Course Name
        input(type='text', name='courseName', value= courses[0].courseName
        br
        button#btnSave(type='button') SAVE
        button#btnDelete(type='button') DELETE

script. 脚本。

 $('#btnDelete').click(function(){
        var action = $(this).val() == '/course/delete/'+courses[0].courseId;
        $('#updateForm').attr('action', action);
        $('#updateForm').submit();
    });
$('#btnSave').click(function(){
    var action = $(this).val() == '/course/update/' + courses[0].courseId;
    $('#updateForm').attr('action', action);
    $('#updateForm').submit();
});

添加按钮类型以提交

button#btnSave(type='submit') SAVE

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

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