javascript - 抓取文本然后将该文本提交到特定路径

标签 javascript jquery

我想做的是抓取一个文本,然后将该文本提交到一个特定的应用程序路径,其中接受表单输入名称=“mykeyword”,我也希望在浏览器的新选项卡中打开它。

我做了一些练习,但没有运气。有什么想法吗?

    $("#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 回调。

$("#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();
  });
});

关于javascript - 抓取文本然后将该文本提交到特定路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46009344/

相关文章:

jquery - 如何更改 css 不显示或单击按钮阻止

javascript - 第一次ajax成功后与其他ajax调用函数

jquery - 如何递归地组织元素(俄罗斯方 block 的碎片)

javascript - 如何以控制台geojson格式纬度/经度输出?

javascript - 如何最小化ajax调用函数

javascript - 位图列表[i]未定义

javascript - jQuery 鼠标每次滚动滚动整个窗口,就像苹果 iphone 5 网站

javascript - gmaps.js 鼠标悬停打开信息窗口

javascript - 如何在 Javascript 中准确计算每日、每周、每月的复利?

javascript - 为什么旧应用程序中的 document.DOM-object.property 为 null