在不破坏我的代码的情况下删除函数

时间:2019-05-11 21:34:18

标签: javascript

我正在尝试在Web应用程序中实现站点搜索功能,但是我想删除多余的单选按钮输入。

原始代码使您可以使用三个单选按钮来选择搜索引擎。我删除了三个按钮,但是我不知道如何删除最后一个按钮。我只想默认使用Google。

// All-in-one Internal Site Search script- By JavaScriptKit.com (http://www.javascriptkit.com)
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// This notice must stay intact for use

//Enter domain of site to search.
var domainroot = "ts.xxxxxxx.net"

var searchaction = [ //form action for the 3 search engines
  "http://www.google.com/search"

]

var queryfieldname = ["q", "p", "q"] //name of hidden query form for the 3 search engines

function switchaction(cur, index) {
  cur.form.action = searchaction[index]
  document.getElementById("hiddenquery").name = queryfieldname[index]
}

function jksitesearch(curobj) {
  for (i = 0; i < document.jksearch.se.length; i++) { //loop through radio to see which is checked
    if (document.jksearch.se[i].checked == true)
      switchaction(document.jksearch.se[i], i)
  }
  document.getElementById("hiddenquery").value = "site:" + domainroot + " " + curobj.qfront.value
}
<form name="jksearch" action="http://www.google.com/search" method="get" onSubmit="jksitesearch(this)">


  <input id="hiddenquery" type="hidden" name="q" />
  <input name="qfront" type="text" style="width: 200px" value="" /> <input type="submit" value="Search" /><br />
  <div style="font: bold 11px Verdana;"><input name="se" type="radio" checked>
  </div>


</form>

2 个答案:

答案 0 :(得分:0)

您可以使用输入type='hidden'并设置value='1'来获得相同的结果。

如果由于某种原因必须将单选按钮保留在表单中,请在父节点上为输入设置display:none;

// All-in-one Internal Site Search script- By JavaScriptKit.com (http://www.javascriptkit.com)
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// This notice must stay intact for use

//Enter domain of site to search.
var domainroot = "ts.kiwiroad.net"

var searchaction = [ //form action for the 3 search engines
  "http://www.google.com/search"

]

var queryfieldname = ["q", "p", "q"] //name of hidden query form for the 3 search engines

function switchaction(cur, index) {
  cur.form.action = searchaction[index]
  document.getElementById("hiddenquery").name = queryfieldname[index]
}

function jksitesearch(curobj) {
  for (i = 0; i < document.jksearch.se.length; i++) { //loop through radio to see which is checked
    if (document.jksearch.se[i].checked == true)
      switchaction(document.jksearch.se[i], i)
  }
  document.getElementById("hiddenquery").value = "site:" + domainroot + " " + curobj.qfront.value
}

// console.log(document.querySelector('input[name="se"]').value);
<form name="jksearch" action="http://www.google.com/search" method="get" onSubmit="jksitesearch(this)">
  <input id="hiddenquery" type="hidden" name="q" />
  <input name="qfront" type="text" style="width: 200px" value="" /> <input type="submit" value="Search" /><br />
  <div style="font: bold 11px Verdana;">
    <input name="se" type="hidden" value="1">
  </div>
</form>

希望这会有所帮助,

答案 1 :(得分:0)

这应该可以解决问题。它具有与原始代码相同的行为,除了没有更多的单选按钮和与之相关的功能之外。

// All-in-one Internal Site Search script- By JavaScriptKit.com (http://www.javascriptkit.com)
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// This notice must stay intact for use

//Enter domain of site to search.
var domainroot = "ts.xxxxxxx.net"

var searchaction = [ //form action for the 3 search engines
  "http://www.google.com/search"

]

function jksitesearch(curobj) {
  document.getElementById("hiddenquery").value = "site:" + domainroot + " " + curobj.qfront.value
}
<form name="jksearch" action="http://www.google.com/search" method="get" onSubmit="jksitesearch(this)">


  <input id="hiddenquery" type="hidden" name="q" />
  <input name="qfront" type="text" style="width: 200px" value="" /> <input type="submit" value="Search" /><br />

</form>