2015-05-16 137 views
0

我有一個確認的腳本彈出window.When我把這個在我們的aspx.cs頁面返回不同values.My腳本確認消息框返回錯誤值

<script type="text/javascript"> 
function Confirm() { 
    var confirm_value = document.createElement("INPUT"); 
    confirm_value.type = "hidden"; 
    confirm_value.name = "confirm_value"; 
    if (confirm("Do you want to remove this employee from this group?")) { 
     confirm_value.value = "1"; 
    } else { 
     confirm_value.value = "2"; 
    } 
    document.forms[0].appendChild(confirm_value); 
} 

按鈕

<asp:Button ID="btnAdd" OnClientClick = "Confirm()" runat="server" ValidationGroup="1" onclick="btnAdd_Click" /> 

按鈕點擊功能

protected void btnAdd_Click(object sender, EventArgs e) 
    { 
    string confirmValue = Request.Form["confirm_value"]; 
     if (confirmValue == "1") 
     { 
      //Do something 
     } 
     else 
     { 
    //Do something 
     } 

    } 

第一次我點擊取消,然後我confirmvalue=="1",然後再次選擇ok,然後我的confirmvalue=="1,2"代替2.它如何返回錯誤值。

回答

4

您正在創建多個名爲「confirm_value」的輸入,每次單擊按鈕時都會輸入一個。你需要做的是重複使用相同的輸入:

function Confirm() { 
    var confirm_value = document.querySelector('[name="confirm_value"]'); 
    if (!confirm_value) { 
     confirm_value = document.createElement("INPUT"); 
     confirm_value.type = "hidden"; 
     confirm_value.name = "confirm_value"; 
     document.forms[0].appendChild(confirm_value); 
    } 
    if (confirm("Do you want to remove this employee from this group?")) { 
     confirm_value.value = "1"; 
    } else { 
     confirm_value.value = "2"; 
    }  
} 
1
function Confirm() { 
var Result=confirm("Do you want to remove this employee from this group?"); 
var confirm_value = document.querySelector('[name="confirm_value"]'); 
if (Result) { 
return true; 
} else { 
return false; 
}  
}