最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Onclick without reload page - Stack Overflow

programmeradmin1浏览0评论

I have 2 url like this:

    <td><a href='infomation/gc_details_2.php?id=<?php echo $row['trans_id']?>'><input type='button' id="yes"  value='Yes'></a></td>
    <td><a href='infomation/del_notifi_2.php?id=<?php echo $row['trans_id']?>'><input type='button' value='No'></a></td>

When onclick button Yes it request to gc_details_2.php and reload all page with search result. Now i want when click button Yes without reload page. How should i do? Thank all

I have 2 url like this:

    <td><a href='infomation/gc_details_2.php?id=<?php echo $row['trans_id']?>'><input type='button' id="yes"  value='Yes'></a></td>
    <td><a href='infomation/del_notifi_2.php?id=<?php echo $row['trans_id']?>'><input type='button' value='No'></a></td>

When onclick button Yes it request to gc_details_2.php and reload all page with search result. Now i want when click button Yes without reload page. How should i do? Thank all

Share Improve this question asked Aug 12, 2015 at 4:40 mrdragonmrdragon 2471 gold badge2 silver badges14 bronze badges 3
  • 1 Both the anchor and button are going to capture clicks, style the anchors to look like buttons instead. – Bankzilla Commented Aug 12, 2015 at 4:45
  • Have you check your console, it gives any error? – Kausha Mehta Commented Aug 13, 2015 at 10:44
  • Trying to clarify your intent, 1) Are you trying to send GET requests based on which button is clicked? If so, what should the browser do when it receives the data from the request(GET request should ideally only get data and not modify anything on server). 2) If you are trying to send some data/update data on server, then it should be a POST/PUT/PATCH/DELETE request. On both the cases, you would be better off having Buttons triggering JS Ajax calls to server as you don't want a page refresh that acpanies anchor. – Edwin Clement Commented Apr 1, 2020 at 7:13
Add a ment  | 

5 Answers 5

Reset to default 1

This is the simplest of all $('#yes').click(function(){ $.get('gc_details_2.php', function(data) { $('html').html(data); }); return false; }); And it Always works

Style your anchor tags to have your desired button effect, and dump the button:

Html:

<a href="infomation/gc_details_2.php?id=<?=$row['trans_id']?>" class="button ajax-fetch">Yes</a>

Javascript:

jQuery(".ajax-fetch").click(function(e){
   e.preventDefault();
   jQuery.get(jQuery(this).attr("href"), function(data) {
      jQuery("#result").html(data);
   });
});

Result container:

<div id="result"></div>

This will catch any object that you click on with the class "ajax-fetch" and follow the href, then load it into the result div. Use some CSS to style the button class.

Use AJAX.

For example:

 $('#yes').click(function() {
    $.ajax({
      url: your_url,
      type: (GET or POST),
      data: {data: your_data},
      success: function(response) {
         alert('Success!');
       }
    });
  });

You should use ajax instead. Also, It's not good practice to use button inside link. Either you go with link or button. I have removed link here and used button.

HTML:

<td><input type='button' id="yes" value='Yes' trans_id="<?php echo $row['trans_id']?>"></td>

JQuery:

$("#yes").click(function(){
    var trans_id=$(this).attr("trans_id");
    $.ajax({
        url:'infomation/gc_details_2.php',
        data:{
            "id":trans_id
        },
        type:"GET",
        success:function(data){
        }
    });
    return false;
});

Try this..

    <td><input type='button' id="yes"  class="<?php echo $row['trans_id']?>" value='Yes'></td>

    $("#yes").click(function(){
        var trans_id=$(this).attr("class");
        $.ajax({
           url:'infomation/gc_details_2.php',
            data:{"id":trans_id},      
            type:"GET",
            success:function(data){
alert("ssfasfas");
            }
        });
    });
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>