2017-02-17 85 views
0

我有一些醜陋的PHP代碼,我將在稍後清理,但首先代碼應該開始工作OK。現在它不執行post命令。換句話說,腳本find.php不會被執行。任何想法如何解決這個問題?我只想使用一個超鏈接。jQuery和PHP編寫的超鏈接onclick

<?php 

    print "<div><div>Play " . $i . "<span><a href=\"#\" onclick=\"$.post('find.php', { 'my': '$my_id', 'your': '$your', 'select': '$i' }); $('html').css({ 'overflow': 'auto', 'height': 'auto' }); this.parentNode.style.display = 'none'; return false;\">Select</a></span></div></div>\n"; 

?> 
+4

沒有試圖聽起來粗魯,但可能可能清理你的代碼首先會使調試更容易?代碼分離並不困難。 – cteski

+0

一些建議:嘗試單獨使用靜態HTML代碼(而不是使用PHP創建的HTML)首先運行'post'。顯然,PHP似乎在這裏服務的唯一目的是使用'$ i','$ my'和'$ your'的值,其餘的都可以是「外部」的PHP。另外,請檢查控制檯是否有任何錯誤。你在那裏看到了哪些通知? –

+0

我認爲它工作正常 –

回答

1

誠實地說,你現在最好清理一下你的代碼。如果你檢查你的控制檯,你很可能會得到關於jQuery沒有被加載的錯誤。

您應該創建所需要的信息適當的按鈕:

<a href="#" class="mybutton" data-my-id="<?php echo $my; ?>" data-your-id="<?php echo $your; ?>" data-select="<?php echo $i; ?>">Select</a> 

,並創建jQuery的事件偵聽器:

jQuery(document).ready(function() { 
    // handle click 
    jQuery(document).on('click', 'a.mybutton', function(e) { 
     e.preventDefault(); 
     var data = { 
      my: jQuery(this).data('my-id'), 
      your: jQuery(this).data('your-id'), 
      select: jQuery(this).data('select'), 
     } 
     //post 
     jQuery.post('find.php', data); 
     alert(data.select); 
     jQuery('html').css({ 'overflow': 'auto', 'height': 'auto' }); 
    this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display = 'none'; return false; 

    }); 
}); 

從那裏,你必須把它清理乾淨,以適應正是你所需要的。如果我是你,我會開始考慮正確的代碼/路徑,而不是「稍後再看」

0

看來你想動態地用超鏈接執行jquery post。從您的代碼

function postFunc(my_id, your, i) { 
    $.post('find.php', { 
    'my': my_id, 
    'your': your, 
    'select': i 
    }); 
} 

function createDynamicDiv(my_id, your, i) 
{ 
    var node = document.createElement('div'); //dynamically creates a div 
    node.setAttribute("style", "overflow: auto; height: auto"); //set css 
    newlink = document.createElement('a'); 
    newlink.setAttribute('class', 'dynamicDiv'); //you might want to group all divs in a common class to add common css 
    newlink.setAttribute('href', 'javascript:postFunc(my_id,your,i)'); //javascript: prefix on the href makes to interpret postFunc as a javascript function 
    node.appendChild(newlink); 

} 

呼叫createDynamicDiv創建一個動態的div - 你完全可以在JavaScript本身實現這一目標。