查找当前表单并更改操作url,然后使用jquery提交

find current form and change action url and submit using jquery

本文关键字:然后 提交 jquery url 操作 表单 查找      更新时间:2023-09-26

在我的JSP中,我有不同的<form>,每个<form>都有<table><th><tr><td>。当我点击<TH>标签时,我们应该找到<form>,更改操作url并在JQUERY click()事件中提交。

我的代码如下:

<form>
    <table>
        <thead>
        <tr>
        <th id='th1'>th1</th>
        <th id='th2'>th2</th>
        <th id='th3'>th3</th>
        <th id='th4'>th4</th>
        <thead>
    </table>
</from

<form>
    <table>
        <thead>
        <tr>
        <th id='th1'>th1</th>
        <th id='th2'>th2</th>
        <th id='th3'>th3</th>
        <th id='th4'>th4</th>
        <thead>
    </table>    
</from>

在这里,当我点击<th id='th1'>时,我们应该找到<form>,更改动作url并提交它。你能帮我吗?

$('th').click(function(){
    var $form = $(this).parents('form');
    $form.attr('action', 'new url').submit();
})

以下是

的示例

例如,您有一个表单

<form>
    <table>
        <thead>
        <tr>
        <th id='th1'>th1</th>
        <th id='th2'>th2</th>
        <th id='th3'>th3</th>
        <th id='th4'>th4</th>
        <thead>
    </table>    
</from>

你可以试试

$("th").click(function(e){   
var thisForm = $(this).closest("form");
thisForm.attr("action","yourAction");
thisForm.submit(); 
});

首先处理HTML:中的问题

  1. 修复关闭的</form>标签(它是"表单",而不是"来自")
  2. 赋予<th>元素独特的id属性

然后是这样的:

$(document).ready(function() {
    $("th").click(function() {
       $(this).closest("form").attr("action", this.id).submit();
    });
});

您没有明确表示要将表单的操作设置为什么,因此作为示例,我将其设置为单击的<th>元素的id。