开发者

Form submission with jQuery not working

开发者 https://www.devze.com 2023-04-04 15:54 出处:网络
I\'m having trouble getting my form submission working. I\'m using a preventDefault in order to dynamically change the action attribute of the form, but when I click

I'm having trouble getting my form submission working. I'm using a preventDefault in order to dynamically change the action attribute of the form, but when I click the button it does not do anything at all, and I'm running out of ideas why this might be. can anyone please have a look and point me to where I went wrong ?

Here's the code:

JS:

           $j('select[id=subtype]').live('change',function(){                 
           var stype = $j(this).val();  
    var styp_pos = $j.inArray(stype, styp_id_arr);
    var styp_url = styp_url_arr[styp_pos];开发者_StackOverflow中文版
    $j('input[id=styp_url]').val(styp_url);
       });


            $j('button[id=frm_submit]').click(function(event){
    event.preventDefault();
    var full_url  = typ_url + '-' + styp_url + '.html';
    alert(full_url);
    $j('form[id=type_sel_form]').attr("action", full_url);
    $j('form[id=type_sel_form]').submit();

});

HTML:

<form id="type_sel_form" name="type_sel" class="stsel_form" action="subtype.php" method="post">
...
<button id="frm_submit" type="submit" class="st_button">View</button>
</form>

EDIT: After having corrected a typo in the first version, and when running the code through Firebug, I get an error message saying that typ_url is not defined. I have defined it during an action (first part of the JS code) which is executed before the submit. Can that be the reason that nothing happens when clicking the button ?

Thanks very much in advance


I'm assuming you've aliased $ to $j. Remove the . after $j in these two lines

$j('#type_sel_form').attr("action", full_url);
$j('#type_sel_form').submit();

Also just providing the id with $('#id') is enough.

Complete example:

$j('#frm_submit').click(function(event){
    event.preventDefault();
    var full_url  = typ_url + '-' + styp_url + '.html';
    alert(full_url);
    $j('#type_sel_form').attr("action", full_url);
    $j('#type_sel_form').submit();
});


Change:

$j.('form[id=type_sel_form]').attr("action", full_url);
$j.('form[id=type_sel_form]').submit();

Into:

$j('form[id=type_sel_form]').attr("action", full_url);
$j('form[id=type_sel_form]').submit();

There are extra dots.

0

精彩评论

暂无评论...
验证码 换一张
取 消