【问题标题】:How to go through the link in html?如何通过html中的链接?
【发布时间】:2016-10-06 13:49:48
【问题描述】:

我有一个小问题。在我的 html 页面中,我有一个文本框,我可以在其中输入 anything i want。当我按 ENTER 时,我想通过下一个 url http://127.0.0.1:8000/search/?request=anythingiwant在 html 代码中是否有方法可以做到这一点?代码在这里

{% extends 'finder/base.html' %}
{% block content %}
	<div class="container">
        <div class="image"></div>
		<h2>Super Search Engine</h2>
        <p>I'm searching for:</h2>
        <input type="text" id="inp" onkeypress="return runScript(event)"> 
    </div>

    <script>
		function runScript(e) {
    		if (e.keyCode == 13) {
        		//var tb = document.getElementById("scriptBox");
        		//eval(tb.value);
        		//return false;
        		alert("kekos");
        		$.ajax({
        			method: 'POST',
        			url: 'http://127.0.0.1:8000/search/',
        			data: {
            			'request': document.getElementById('inp').value,
        			},
        			success: function(data){
            			$scope.key = data['key'];
            			$scope.isAuthorized = true;
            			$window.alert('You are logged in.');
        			},
        			error: function(data){
            			$window.alert("Something was wrong.");
        			},
      			})
    		}
		}
	</script>
{% endblock content %}

【问题讨论】:

  • 您正在寻找&lt;form&gt;
  • 一个表单元素和提交按钮?
  • 应该是type: "GET" 而不是method: "POST"
  • @Barmar 试过了,没用。

标签: javascript html django http hyperlink


【解决方案1】:

如果您想从新 URL 重新加载,请分配给 window.location 而不是发送 AJAX 请求。

if (e.keyCode == 13) {
    window.location = "http://127.0.0.1:8000:/search/?" + encodeURIComponent(event.target.value);
}

【讨论】:

    【解决方案2】:

    如果您只需要将文本输入值作为参数发送到其他页面,您可以使用 form action(description at W3C)

    示例:

    <form method="get" action="/search/">
      <input type="text" name="request" />
      <input type="submit" value="submit">
    </form>
    

    如果您不需要提交按钮,只需在表单上用 JS 触发 submit 事件(例如 onkeydown)

    【讨论】:

      【解决方案3】:

      如果我正确理解您的问题。你可以这样做:

      function runScript(e) {
                  if (e.keyCode == 13) {
                      var tb = document.getElementById("np").value;
                      //eval(tb.value);
                      //return false;
                      alert("kekos");
                      $.ajax({
                          method: 'POST',
                          url: 'http://127.0.0.1:8000/search/?request='+tb,
                          success: function(data){
                              $scope.key = data['key'];
                              $scope.isAuthorized = true;
                              $window.alert('You are logged in.');
                          },
                          error: function(data){
                              $window.alert("Something was wrong.");
                          },
                      })
                  }
              }
      

      【讨论】:

      • 什么是scriptBox?为什么要同时在 URL 参数和data: 选项中发送请求数据?
      • 如果你想加载这个新的 url,你应该使用 barmar 写的 window.location。
      猜你喜欢
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 2010-11-10
      • 2021-01-11
      • 2013-02-02
      • 2021-05-22
      • 1970-01-01
      • 2011-09-22
      相关资源
      最近更新 更多