简体   繁体   中英

JavaScript Variable Retaining Last Input

I have a ButtonLink added to a asp:gridview that displays a model pop-up window with a notification message and the text from cell[1] of the row that was clicked. For 98% of them there is the default standard message, which is the cell[1] value, but for these few caveats I want to display a custom message based off the value in cell[1].

My issue I have with this syntax is that it always shows

Gi Joe Destory Cobra

regardless of what the actual text for row.cells[1] is. Can someone assist in a re-write so it will function as I want?

<script type='text/javascript'>
     var row;
     function GetSelectedRow(lnk) {
         row = lnk.parentNode.parentNode;
         var rowIndex = row.rowIndex - 1;
         if (row.cells[1].innerHTML = "Green Lantern") {
             desc = "Please input villian."
         }
         if (row.cells[1].innerHTML = "Batman") {
             desc = "Stay out of Gotham."
         }
         if (row.cells[1].innerHTML = "Gi Joe") {
             desc = "Destroy Cobra."
         }
         else { desc = row.cells[1].innerHTML; }
         $('#<%=lblText.ClientID %>').text(desc);
         $('#myModal').modal('show');
     }
</script>

EDIT
I altered my code to this

<script type='text/javascript'>
     var row;
     function GetSelectedRow(lnk) {
         row = lnk.parentNode.parentNode;
         var rowIndex = row.rowIndex - 1;
         if (row.cells[1].innerHTML == "Green Lantern") {
             desc = "Please input villian."
         }
         if (row.cells[1].innerHTML == "Batman") {
             desc = "Stay out of Gotham."
         }
         if (row.cells[1].innerHTML == "Gi Joe") {
             desc = "Destroy Cobra."
         }
         else { desc = row.cells[1].innerHTML; }
         $('#<%=lblText.ClientID %>').text(desc);
         $('#myModal').modal('show');
     }
</script>

But for example if user clicks the ButtonLink for Gi Joe instead of desc displaying Descroy Cobra it displays GI Joe.

make sure difference between if condition and else if

     if (row.cells[1].innerHTML == "Green Lantern") {
         desc = "Please input villian."
     }
     else if (row.cells[1].innerHTML == "Batman") {
         desc = "Stay out of Gotham."
     }
     else if (row.cells[1].innerHTML == "Gi Joe") {
         desc = "Destroy Cobra."
     }
     else { desc = row.cells[1].innerHTML; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM