使用 jQuery 提交表单不起作用
我无法正常提交表单。我正在使用 PreventDefault 为了动态更改表单的操作属性,但是当我单击 该按钮根本不执行任何操作,我已经不知道为什么会这样了 是。任何人都可以看一下并指出我出错的地方吗?
代码如下:
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];
$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>
编辑: 纠正第一个版本中的拼写错误后,通过 Firebug 运行代码时,我收到一条错误消息,指出typ_url 未定义。我已经在提交之前执行的操作(JS 代码的第一部分)期间定义了它。这可能是单击按钮时没有任何反应的原因吗?
预先非常感谢
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];
$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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您已将
$
别名为$j
。删除这两行中$j
之后的.
另外只需提供
id
和$('#id')
就足够了。完整示例:
I'm assuming you've aliased
$
to$j
. Remove the.
after$j
in these two linesAlso just providing the
id
with$('#id')
is enough.Complete example:
更改:
变为:
有多余的点。
Change:
Into:
There are extra dots.