我正在用JavaScript进行搜索。我会使用一个表单,但它在我的页面上搞砸了其他东西。我有这个输入文本字段:

<input name="searchTxt" type="text" maxlength="512" id="searchTxt" class="searchField"/>

这是我的JavaScript代码:

<script type="text/javascript">
  function searchURL(){
    window.location = "http://www.myurl.com/search/" + (input text value);
  }
</script>

我如何从文本字段的值变成JavaScript?


当前回答

function handleValueChange() { var y = document.getElementById('textbox_id').value; var x = document.getElementById('result'); x.innerHTML = y; } function changeTextarea() { var a = document.getElementById('text-area').value; var b = document.getElementById('text-area-result'); b.innerHTML = a; } input { padding: 5px; } p { white-space: pre; } <input type="text" id="textbox_id" placeholder="Enter string here..." oninput="handleValueChange()"> <p id="result"></p> <textarea name="" id="text-area" cols="20" rows="5" oninput="changeTextarea()"></textarea> <p id="text-area-result"></p>

其他回答

//creates a listener for when you press a key
window.onkeyup = keyup;

//creates a global Javascript variable
var inputTextValue;

function keyup(e) {
  //setting your input text to the global Javascript Variable for every key press
  inputTextValue = e.target.value;

  //listens for you to press the ENTER key, at which point your web address will change to the one you have input in the search box
  if (e.keyCode == 13) {
    window.location = "http://www.myurl.com/search/" + inputTextValue;
  }
}

请在代码依赖中查看此功能。

你也可以,通过标签名称调用,像这样: 所以你会得到特定形式的特定输入值。

简单的JavaScript:

function copytext(text) {
    var textField = document.createElement('textarea');
    textField.innerText = text;
    document.body.appendChild(textField);
    textField.select();
    document.execCommand('copy');
    textField.remove();
}

你应该能够输入:

var input = document.getElementById(“searchTxt”); 函数搜索URL() { window.location = “http://www.myurl.com/search/” + input.value; } <input name=“searchTxt” type=“text” maxlength=“512” id=“searchTxt” class=“searchField”/>

我相信有更好的方法可以做到这一点,但这一方法似乎可以跨所有浏览器工作,并且它只需要对JavaScript有最少的了解就可以进行制作、改进和编辑。

function searchURL() {
   window.location = 'http://www.myurl.com/search/' + searchTxt.value
}

基本上就是searchTxt。value将返回id='searchTxt'的输入字段值。