我希望这个脚本触发javascript函数而不是打招呼

时间:2016-02-22 19:01:58

标签: javascript jquery

我找到了一个有效的脚本,现在请记住,我根本不了解JQuery。我想要这个脚本:

document.getElementById("id_of_button").onclick = function() {clickFunction()};

function clickFunction() {alert("hello");}

$("#id_of_textbox").keyup(function(event){
    if(event.keyCode === 13){
        $("#id_of_button").click();
    }
});

触发此功能而不是创建hello弹出窗口

document.location.href = 'http://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' + escape(document.getElementById('search-box').value)

3 个答案:

答案 0 :(得分:0)

不完全清楚,但来自有限的信息:

function clickFunction() {
  var mylink = escape(document.getElementById('search-box').value);
  document.location.href = 'http://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' + mylink;
}

看起来整个事情可以写成:

$("#id_of_button").on('click', function() {
  var mylink = escape($('#search-box').value);
  document.location.href = 'http://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' + mylink;
});

$("#id_of_textbox").keyup(function(event) {
  if (event.keyCode === 13) {
    $("#id_of_button").trigger('click');
  }
});

答案 1 :(得分:0)

下面是您想要的代码

<input type="text" id="search-box">
<button id="id_of_button">Search</button>

<script type="text/javascript">
  document.getElementById("id_of_button").onclick = function() {clickFunction()};

  function clickFunction() {
    document.location.href =
        'https://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' +
      escape(document.getElementById('search-box').value)
    }

  $("#id_of_textbox").keyup(function(event){
      if(event.keyCode === 13){
          $("#search-box").click();
      }
  });
</script>

答案 2 :(得分:0)

$("#id_of_textbox").click(function () {
    document.location.href = 'http://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' + escape($('#search-box').val())
});

$(document).keypress(checkKey);

function checkKey(e) {
    switch (e.keyCode) {
    case 13:
       document.location.href = 'http://cse.google.com/cse?cx=009002930969338329916:kta6o_isob0&q=' + escape($('#search-box').val())
        break;
    }
}