使用jquery提交表单不能按预期工作

时间:2013-01-01 08:30:52

标签: jquery

我正在尝试在修改它的action属性后使用jquery提交表单。 action属性更新正常,但我希望浏览器位置与操作相同。但事实并非如此。你知道为什么吗?

以下是表格:

<form method="GET" class="modForm" action="">
<input type="text" placeholder="Search" class="modSearchValue">
<input type="radio" name="text" value="text" class="text" title="Search">
</form>

这是jquery:

$('.modForm').submit(function(event) {

        var $this = $(this),

        var query = $this.find('.modSearchValue').val(); // Use val() instead of attr('value').
        var locale = window.location;
         if($('.text').is(':checked')){query = '&text='+query;}else{query = '&handle='+query;}
        route = locale+query;
        console.log(route);

        if (query.length >= 1) {
        // Use URI encoding
        var newAction = (route);
        console.log(newAction); // DEBUG
        // Change action attribute
        $this.attr('action', newAction);
        //event.preventDefault();
        } else {
        console.log('Invalid search terms'); // DEBUG
        // Do not submit the form
        event.preventDefault();
        }
    });

此处,如果当前位置为http://localhost/search且搜索字词为14,则操作将更改为http://localhost/search/?handle=14,然后提交。但出于某种原因,它不会。

1 个答案:

答案 0 :(得分:2)

您必须使用<form method="POST" class="modForm" action="">方法POST才能获得它.......

删除逗号var $this = $(this),并使用var $this = $(this);