jQuery anchor preventDefault [英] jQuery anchor preventDefault

查看:55
本文介绍了jQuery anchor preventDefault的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery,如何从文本框中获取值,然后根据值加载新页面?

Using jQuery, how do I get the value from a textbox and then load a new page based on the value?

例如,假设文本框包含你好在page1.php上,如何更改锚标记的默认行为,现在加载以下内容

For example, lets say the textbox contains "hello" on page1.php, how do I change the default behavior of an anchor tag to now load the following

page2.php?txt = hello

page2.php?txt=hello

到目前为止我有以下内容:

I have the following so far:

<script type="text/javascript">
    $(document).ready(function(){
    $("a.mylink").click(function(event){
        alert("link clicked");
        event.preventDefault();
    });
});
</script>


推荐答案

Html

    <input type="text" id="demoQuery" />
    <a id='demoLink' href='javascript:'>Iam a link</a>

Javascript

jQuery('#demoLink').bind('click',function(e){
      e.preventDefault();
      document.location.href="someUrl.php?text="+ jQuery('#demoQuery').val();     
});

这篇关于jQuery anchor preventDefault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆