2010-10-16 50 views
0

即時通訊在javascript中很新,所以這是個問題。每當用戶點擊網頁上的特定圖像時,我需要進行POST操作,每個圖像發送一個POST變量,如「image1 = 1」...當用戶點擊圖片時需要發帖子

例如:在this頁面中,每次點擊號碼進行投票,它會發起一個後續操作,我需要相同的圖像和解碼代碼。 請幫我看看代碼。謝謝!

請注意,當您在圖像中使用按鈕時,IE出現問題,因此解決方案無法通過圖像按鈕實現。

+1

什麼是與Internet Explorer和圖像按鈕的問題? – 2010-10-16 01:12:14

+1

我在我的應用程序中使用imagebuttons,並且他們似乎在IE(至少6/7/8)中也能正常工作。你可以針對你看到的問題和IE的哪個版本更具體些嗎? – InSane 2010-10-16 01:13:32

+1

''以不同的方式發送參數名稱。實際上會發送兩個參數,一個是'.x',另一個是'.y'。我認爲OP在服務器端依賴參數名稱時根本不知道這一點。 – BalusC 2010-10-16 01:17:08

回答

1

如果我正確理解你的問題,JavaScript不需要來實現你想要的。

我建議使用常規按鈕並將其設置爲圖像樣式。

像這樣的東西應該工作:

input.imgbutton { 
    background-image: url(...); 
    background-color: inherit; 
    border: none; padding: none; margin: none; 
    height:32px; width:32px; 
    text-indent:-9999px; 
} 

演示在這裏:http://jsbin.com/iwame3/edit

+0

怎麼樣? – 2013-07-17 13:36:33

1

既然你是JavaScript的新手,你可能會考慮使用jQuery來達到這個目的。如果是這樣,我建議閱讀jQuery.clickjQuery.post的API文檔。

+1

OP在哪裏提到jQuery? :) – Marko 2010-10-16 01:35:00

+0

使用javascript和jQuery最重要的是,可以在沒有任何腳本的情況下實現純粹的文體效果似乎並不是正確的方向。 – 2010-10-16 01:40:40

+0

OP不但沒有提到jQuery,還沒有在他鏈接的頁面上使用它。所以這個解決方案會在他之前沒有的項目中引入另一個依賴項。 – 2010-10-16 01:43:08

1

這些問題都與被稱爲AJAX一個偉大的JavaScript功能解決。與JavaScript Framework Prototype一個例子:

<img src="/path/to/image" id="image-1" /> 
<img src="/path/to/image" id="image-2" /> 
<img src="/path/to/image" id="image-3" /> 
<!-- ... etc. --> 

<script type="text/javascript"> 
    var url = '/relative/path/to/send/post/request'; 

    // Get all images and start observing the click event 
    // to start a AJAX request 
    $$('img').invoke('observe', function(image) { 

     // Extract number from image id 
     var id = image.id.match(/\d+$/)[0]; 

     // Send the request with the post paremeter image=id 
     new Ajax.Request(url, { 
      method: 'post', 
      parameters: {image: id}, 
      onSuccess: function(response) { 
       alert('Jippii, successfully sending post request'); 
       // ... 
      } 
     }); 

    }); 
</script> 

更多的相關信息爲Ajax.Requesthttp://api.prototypejs.org/ajax/ajax/request/

請記住,它確實是方便,快捷,建立這樣的功能與JavaScript框架(jQuery的,原型,MooTools的,等等) 。

1

HTML:

<form id="radio_form"> 
    <fieldset> 
     <label><input class="myRadio" type="radio" name="color" value="1" checked="checked" />1</label><br /> 
     <label><input class="myRadio" type="radio" name="color" value="2" />2</label><br />  
     <label><input class="myRadio" type="radio" name="color" value="3" />3</label><br /> 
     <label><input class="myRadio" type="radio" name="color" value="4" />4</label><br /> 
     <label><input class="myRadio" type="radio" name="color" value="5" />5</label><br /> 
     </fieldset> 
    </form> 

    <br /> 
    <br /> 

    <img class="CanVote" src="myImage78007.png" alt="myImage78007"> 

的JavaScript(jQuery的):

var gFileToPostTo='VoteImage.aspx'; //Location of the file you want to send the POST Request to 


    function VoteImage(pVote,pImage) { 
     $.post(gFileToPostTo, { vote: pVote, image: pImage}); //We send POST request to the file, which is set above 


    } 

    $(document).ready(function(){ 
      $('img .CanVote').click(function() { //if the client, click on one of the image so have class "CanVote".... 
       VoteImage(1,$(this).src); // Will it send a POST request to the file that is set above, with a vote 1 

      }); 

      $('.myRadio').click(function() { //If the client , click on one of the radio buttons .. 

       var vote = $(this).val(); 
       $('img .CanVote').each(function(i) { 
       VoteImage(vote,$(this).src); //Will the vote be sent as POST to the file that has been set above. With the vote they chose to all the images. This is repeated for all images on the page that has CanVote class 

       }); 
      } 
    } 

有關發佈的詳細信息在jQuery的: http://api.jquery.com/jQuery.post/

0

有使用AJAX這裏各種各樣的回答。

聽起來你可能想提交一個正常的頁面重新加載,但也許這種方法是好的。

通過將錨元素的onclick監聽器綁定到提交表單的函數,您可以使用表單的submit方法來提交它。

<a href='#' onclick='cow_submit("zoodle")'><img src='send.gif'></a> 
<form method='post' id='formie' action='find_some_action.php'> 
    <input type='hidden' id='snoutvar' name='snoutvar' value='snout'> 
</form> 

<script> 
function form_submit(a_var_to_set){ 
    var form_element=document.getElementById('formie'); //get the element as a JS object 
    var snout=document.getElementById('snoutvar'); //get the hidden input element 
    snout.value=a_var_to_set; //set the value to whatever you want 
    form_element.submit(); //sends the form. the page will reload 
    } 
</script>