单击buttonimage应该转到search.php

时间:2012-10-03 08:16:16

标签: php javascript

<script>
    function loadXMLDoc() {
        $("input[class=search]").bind("keyup",function(){
            $.get("search.php?search="+$(this).val(), 
                  function(data){
                      if(data==1) {
                          alert("this is valid search")
                      } else {
                          alert("this is a right user search");
                      }
                  }
            );
        });
    }
</script>

下面是按钮图片代码

<table width="165" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <img src="images/search_box_left_im.png" width="3" height="28" />
        </td>
        <td class="inputbox_bg" width="118px">
            <input type="text" name="search" class="username">
        </td>
        <td>
            <input type="image" onclick="loadXMLDoc()" src="images/search_go_btn.png" border="0" width="44" height="28" />
        </td>
    </tr>
</table>

它将转到函数但不执行ajax代码

4 个答案:

答案 0 :(得分:1)

改变这个:

<script>
function loadXMLDoc()
{
$("input[class=search]").bind("keyup",function(){
 $.get("search.php?search="+$(this).val(),function(data){
    if(data==1){
      alert("this is valid search")
    }else{
      alert("this is a right user search");
    }
})

})
}
</script>

到此:

<script>
function loadXMLDoc()
{
$("input[name=search]").bind("keyup",function(){ //<- important bit here
 $.get("search.php?search="+$(this).val(),function(data){
    if(data==1){
      alert("this is valid search")
    }else{
      alert("this is a right user search");
    }
})

})
}
</script>

答案 1 :(得分:0)

$(document).ready(function(){

$(".username").keyup(function(event){ //<- important bit here
 $.get("search.php?search="+$(this).val(),function(data){
    if(data==1){
      alert("this is valid search")
    }else{
      alert("this is a right user search");
    }

   });
});

$('input[type=image]').click(function() {
  $('.username').keyup();
});
});

答案 2 :(得分:0)

请试试这个:

<script>
function loadXMLDoc() {
    $.ajax({
        type: 'GET',
        url: 'search.php?search='+$(".username").val(),
        success: function(data){                
          if(data==1) {
              alert("this is valid search")
          } else {
              alert("this is a right user search");
          }  
        }
    });
}
</script>

答案 3 :(得分:0)

您的<input type="image" ... />是表单的提交按钮。 你想要做的是将其默认行为(提交表单)更改为替代 - 执行一些代码。 以下是一个很好的问题:event.preventDefault() vs. return false

除此之外,您要分配两次事件监听器 - 一次在<input onclick="" />中,第二次在事件处理程序$("input[class=search]").bind()

尝试在<input onclick="" />中删除$(document).ready()并定义事件监听器:

<script>
$(document).ready(function() {
    $("input[class=search]").bind("keyup",function(e){
        e.preventDefault();
        $.get("search.php?search="+$(this).val(),function(data){
            if(data==1){
                alert("this is valid search")
            }else{
                alert("this is a right user search");
            }
        });
    });
});
</script>
...
<input type="image" src="images/search_go_btn.png" border="0" width="44" height="28" />
...