2016-03-11 2 views
0

Я пытаюсь обменять изображения спереди/сзади onclick(). Когда я нажимаю изображение, я вижу заднее изображение, но когда я нажимаю на заднее изображение, я не вижу фронта. Я передаю передние и задние пути от серверного кода:Обмен фотографиями на странице Asp.net

<script> 


var theImg = null, theImgPath = null; 

window.addEventListener("load", function() { 
    theImg = document.getElementById("imgCh_Front"); 
    theImgPath = theImg.getAttribute("src"); 
}); 

function changeImage(front, rear) { 
    if (theImgPath === front) { 
     alert("1"); 
     theImg.src = rear; 
     alert("2"); 
    } else if (theImgPath === rear) { 
     alert("3"); 
     theImg.src = front; 
     alert("4"); 
    } else { 
     alert("else.."); 
    } 
} 

HTML

<div class="pic_box"> 
        <img 
         onclick="changeImage('<%# GetFileURL(Eval("ImageFront"))%>', '<%# GetFileURL(Eval("ImageRear"))%>')" 
         id="imgCh_Front" alt="" class="ImageCh" src='<%# GetFileURL(Eval("ImageFront"))%>' /> 
        <div class="ico"> 
         <span class="fa fa-photo"></span> 
        </div> 
        <div class="shade"></div> 
       </div> 

функция

Public Shared Function GetFileURL(fileID As String) As String 
     Dim fileInfo = GetFile(Convert.ToInt32(fileID)) 
     ' return a bad or default image url as appropriate 
     If fileInfo Is Nothing Then 
     Else 
      Return GetUrl(fileInfo) 
     End If 
    End Function 
+0

Я думаю, вам не хватает SRC в else if (document.getElementById ("imgCheque_Front") = imgRear) { Это должно быть else if (document.getElementById ("imgCheque_Front"). Src = imgRear) { –

+0

Вы используете переменное присваивание '=' в вас, если блокировать, когда y ou нужно использовать оператор сравнения '=='. – scrappedcola

+0

@scrappedcola Если я использую ==, то это вообще не работает. – alwaysVBNET

ответ

2

Этот код фиксирует две проблемы, которые у вас есть, которые не получали источник изображения в одном случае и не сравнивали источник, а скорее присваивали s в другом.

Это также удаляет повторяющиеся вызовы найти SRC на изображение в:

<script> 


    var theImg = null, theImgPath = null; 

    window.addEventListener("load", function(){ 
     theImg = document.getElementById("imgCheque_Front"); 
    }); 

    function changeImage(front, rear) { 

     theImgPath = theImg.getAttribute("src"); 

     if (theImgPath === front) { 
      theImg.src = rear; 
     } else if (theImgPath === rear) { 
      theImg.src = front; 
     } else { 
      alert("else.."); 
     } 
    } 
</script> 

Как и в сторону, ваш код на стороне сервера есть некоторые вопросы:

' The function is declared as returning a String 
Public Shared Function GetFileURL(fileID As String) As String 

    ' But here, your Dim doesn't have an "As" clause, which 
    ' indicates that your are not working with Option Explicit 
    ' and/or Option Strict turned on (very bad). 

    ' What type does GetFile return? That's the type that fileInfo 
    ' should be declared as. 
    Dim fileInfo As ??? = GetFile(Convert.ToInt32(fileID)) 

    ' Here, you have an if/else with an empty true branch (not 
    ' a good convention): 
    If fileInfo Is Nothing Then 
    Else 
     Return GetUrl(fileInfo) 
    End If 

    ' How about this instead of all that above code: 
    If Not GetFile(Convert.ToInt32(fileID)) Is Nothing Then 
     Return GetUrl(fileInfo) 
    End If 

End Function 
+1

Давайте продолжим обсуждение в чате (http://chat.stackoverflow.com/rooms/106039/discussion-between-scott-marcus-and-alwaysvbnet). –

+0

отличные рекомендации – alwaysVBNET

Смежные вопросы