找到当前表单并更改操作URL并使用jquery提交

时间:2012-02-09 06:01:38

标签: javascript jquery-ui jquery

在我的JSP中,我有不同的<form>,每个<form>都有<table><th><tr><td>。当我点击<TH>代码时,我们应该找到<form>,更改操作网址并在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>,更改操作网址并提交。你能帮我吗?

3 个答案:

答案 0 :(得分:2)

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

Here is an example

答案 1 :(得分:1)

说,例如你有一张表格

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

});

答案 2 :(得分:0)

首先要处理HTML中的问题:

  1. 修复您的结束</form>代码(它是“形式”,而非“来自”)。
  2. 为您的<th>元素提供唯一的id属性。
  3. 然后是这样的:

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

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

相关问题