jQuery将表单提交到新选项卡? [英] jQuery to submit form to new tab?

查看:104
本文介绍了jQuery将表单提交到新选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动作链接,该链接的格式需要每分钟更新一次令牌.当用户单击提交"按钮时,我从api调用中获得了新的url/令牌.我正在使用类似的东西

I have an action link in a form that needs to be updated with a token every minute. I get the new url/token from an api call when the user clicks on the submit button. I'm using something like this

 <form id="somelink" action="http://some.external.url/" target="_blank" />

 $('#somebutton').click(function(e) {
      e.preventDefault();
      var url = '/apicall?id=' + $('#someid').val();                  
      $.post(url, function(data) {
           $('#somelink').attr('action', data);
           $('#somelink').submit();

问题是,提交时使用js/jQuery,它会在新窗口中打开,而浏览器中没有典型的菜单和按钮,这是弹出窗口的大小.

problem is, using js/jQuery when it submits it opens in a new window without the typical menu and buttons in the browser and it's the size of a popup window.

如果没有jQuery,如果我只是单击按钮,它将在新选项卡中打开.

Without the jQuery if I just clicked on the button it'll open in a new tab.

如何使用jQuery完成此操作?

How can I accomplish this with jQuery?

推荐答案

代替捕获click事件,请尝试捕获表单的submit事件,如果发现任何错误,可以使用event.preventDefault( );
更新 在这里,我使用此代码进行了测试,并且工作正常,

Instead of catching the click event, try catching the submit event of the form and in case you find any error you can use event.preventDefault();
Update Here i tested with this code and it's working fine,

<!doctype html />
<html>
  <head>        
    <title></title>
    <script type="text/javascript" src="jquery.js"/></script>
</head>
<body>
  <form action='http://someSite.com' id='somelink' target='_blank'>
    <input type='text' name='lol' id='lol'/>
    <input type='submit'/>
  </form>
  <script type="text/javascript"/>
      $('#somelink').on('submit',function(e) {
          if($('#lol').val() == "poop"){ //Sample error check
              e.preventDefault();
              alert($('#lol').val());
          }
      });
  </script>
</body>
</html>

这是什么,如果您输入"poop",则不提交表单,否则将其提交到新选项卡.希望这会有所帮助:)
更新:
关于您的评论,如果出现错误,您想打开其他标签,然后将e.preventDefault()替换为$(this).attr({"action":"http://www.someothersite.com"});.希望这会有所帮助:)
更新:
如果要异步打开选项卡,则将很困难,这是它可以获得的最接近的时间.
HTML零件

What this does is, if you enter "poop" it does not submit the form, for anything else it submits the form to new tab. Hope this helps :)
Update:
Regarding your comment, if in case of error you want to open some other tab, then replace e.preventDefault() with $(this).attr({"action":"http://www.someothersite.com"}); . Hope this helps :)
Update:
If you want to open a tab asynchronously, then you would have a hard time, this is the closest it can get.
HTML Part

<form id="someForm" action="about:blank" target="newStuff" />
<input type="submit" value="12345" id="someid" />

Javascript部分

Javascript Part

$(document).ready(function() {
 $('#someForm').submit(function(e) {
    $.ajax({
        url: 'http://search.twitter.com/search.json?',
        dataType: 'jsonp',
        success: function(data) {
            if (data != "") {     
                var link = "http://www.google.com/";
                window.open(link,'newStuff'); //open's link in newly opened tab!
            }
        }
    });
 });
 });

这篇关于jQuery将表单提交到新选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆