单击“输入”时,图像标记onclick事件不起作用

时间:2015-08-14 16:32:14

标签: javascript jquery html onclick onclicklistener

嗨,谢谢你的帮助!

我目前正在使用SharePoint 2010.我有一个搜索框,它使用图像onclick()事件来触发搜索。单击图像图标时效果很好但在文本框中单击输入一次时根本不起作用。这是按钮的代码。

<div style="position:relative; top:-3px; ">
  <label for="Search"><b>SEARCH</b>
  </label>&nbsp;&nbsp;&nbsp;
  <label for="Content">
    <input id="Content" type="radio" name="radiou" value="URL" checked="checked" style="margin-bottom:5px" />Content</label>
  <label for="Employees">
    <input id="Employee" type="radio" name="radiou" value="URL" style="margin-bottom:5px" />Employee</label>
  <input id="search_txt" title="Enter search words" type="text" name="k" maxlength="80" size="25" />&nbsp;
  <img src="/Style Library/Images/Search_button.png" onclick="submitSearch()" alt="Search Button" style="margin:0px; position:relative; top:7px; width:30px;"></img>
</div>

这是在点击按钮时激活代码的javascript。我想在键入一个术语并单击Enter键后也会被解雇。

< script type = "text/javascript"
language = "JavaScript" >
  // <![CDATA[  
  function submitSearch() {
    var radiovals = document.getElementsByName("radiou");
    var hostnameforsearch = window.location.hostname;
    if (radiovals[0].checked) {
      queryVal = $('#search_txt').val();
      var searchUrl = 'http://' + hostnameforsearch + '/search/results.aspx?k=' + queryVal + '&u=' + hostnameforsearch + '&cs=This Site';
      window.location = searchUrl;
      return false;
    } else {
      queryVal = $('#search_txt').val();
      window.open('http://search.URL.com/Pages/PeopleResults.aspx?k=' + queryVal);
    };

  }; // ]]>
< /script>

感谢您的帮助! 肯

4 个答案:

答案 0 :(得分:0)

使用javascript检测按下的回车键然后提交

$(document).ready(function(){
$("#search_txt").on("keyup",function(e){
    var code = (e.keyCode ? e.keyCode : e.which);
     if(code == 13) { //Enter keycode
       submitSearch();
     }
    });
});

演示:https://jsfiddle.net/farhanbaloch/devevwwy/

答案 1 :(得分:0)

使用<form>代替<div>,并将事件处理程序附加到表单上的submit事件。

答案 2 :(得分:0)

嗯,很简单,你没有在输入中提及submitSearch。

<input id="Employee" type="radio" name="radiou" value="URL" style="margin-bottom:5px" onkeydown="keyPressed(event)"/>Employee</label>

然后在你的代码中

function keyPressed(e){
   if(e.which==13){
     submitSearch()
   }
}

答案 3 :(得分:0)

这对我有用......

的Javascript

function submitSearch() {
  var radiovals = document.getElementsByName("radiou");
  var hostnameforsearch = window.location.hostname;
  if (radiovals[0].checked) {
    queryVal = $('#search_txt').val();
    var searchUrl = 'http://' + hostnameforsearch + '/search/results.aspx?k=' + queryVal + '&u=' + hostnameforsearch + '&cs=This Site';
    window.location = searchUrl;
    return false;
  } else {
    queryVal = $('#search_txt').val();
    window.open('http://search.URL.com/Pages/PeopleResults.aspx?k=' + queryVal);
  };

};

$(document).ready(function() {
  $("#search_txt").on("keyup", function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) { //Enter keycode
      submitSearch();
    }
  });
});

缺少事件处理程序

<img src="/Style Library/Images/Search_button.png" onclick="submitSearch()" onkeydown="keyPressed(event)" alt="Search Button" style="margin-bottom:0px; width:30px;"></img>