抓取文本,然后将该文本提交到特定路线 [英] grab a text then submit that text to a specific route

查看:72
本文介绍了抓取文本,然后将该文本提交到特定路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是抓取文本,然后将该文本提交到应用程序的特定路径,在该路径中,输入名称为"mykeyword"的形式被接受,我也希望在浏览器的新标签页中打开该文本.

I'm trying to do is grab a text then submit that text to a specific route of application where form input name="mykeyword" is accepted and i also want this open in new tab in browser.

我做了一些练习,但是没有运气.有什么主意吗?

I did some practice but no luck. Any idea?

    $("#ScanTitle").click(function () {
                var mykeyword = $("#mykeyword").text();
                // the 'mykeyword' variable will be submitted to 'Search/Index' route with INPUT name='mykeyword' form attribute in new window
                $.post('Search/Index', function (data) {
                
                var w = window.open('about:blank', 'windowname');
                w.document.write(data);
                w.document.close();
                });
            
            });

    <!DOCTYPE html>
    <html>
    <head>
        <title>test</title>
    
        <script
      src="https://code.jquery.com/jquery-2.2.4.min.js"
      integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
      crossorigin="anonymous"></script>
    
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    
    </head>
    <body>
    
    <button id="ScanTitle">button</button>
    <p id="mykeyword">hand bags</p>
    
   
    
    </body>
    </html>

推荐答案

您需要向$.post提供参数:

$.post('Search/Index', {name: myKeyword}, function(data) {
    var w = window.open('about:blank', 'windowname');
    w.document.write(data);
    w.document.close();
});

要绕过弹出窗口阻止程序,您需要在点击处理程序中打开窗口,而不是AJAX回调.

To get around the popup blocker, you need to open the window in the click handler, not the AJAX callback.

$("#ScanTitle").click(function() {
  var mykeyword = $("#mykeyword").text();
  var w = window.open('about:blank', 'windowname');
  $.post('Search/Index', {name: myKeyword}, function(data) {
    w.document.write(data);
    w.document.close();
  });
});

这篇关于抓取文本,然后将该文本提交到特定路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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