如何在Java脚本中限制追加字符串 [英] How to restrict append string in Java script

查看:55
本文介绍了如何在Java脚本中限制追加字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Frinds,



我正在使用javascript。我在del OnclintClick事件上调用了javascript函数。当我点击del按钮后面的代码时。当我点击确定时它会给我确认框消息它返回YES。但第二次点击确定它。追加字符串如Ex是的,是的

我怎么能限制追加它。

我的代码是 -

Javascript代码--- -



Hello Frinds,

I am using javascript. I call javascript function on del OnclintClick event. when I clicked on code behind on del button. it's give me Confirm box msg when I click on ok it's return YES. but second time Click on Ok its. append string like Ex "Yes,Yes"
how could I restrict to append it.
My Code Is -
Javascript code ----

function Confirm() {
            var confirm_value
            confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";

            if (confirm("Do you want to delete supplier ?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }

            document.forms[0].appendChild(confirm_value);
        }





C#代码----------



C# Code----------

string confirmValue = string.Empty;
                    confirmValue = Request.Form["confirm_value"];
                    if (confirmValue == "Yes")
                    {
                        string str = string.Empty;
                        int rowIndex = Convert.ToInt32(e.CommandArgument);
                        ViewState["Index"] = rowIndex;
                        int Id = Convert.ToInt32(grdTenDistSupp.Rows[rowIndex].Cells[0].Text);
                        str = ClsResponse.DeleteTenderDistributionById(Id);
                        if (str == "SUCCESS")
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Supplier deleted successfully ');", true);
                            TenderDistributionSuppliersByTCId();

                        }
                    }





谢谢

Rameshwar



Thanks
Rameshwar

推荐答案

这里的问题是您在多次点击时反复追加相同的元素。您可以在添加新元素之前删除之前添加的元素,例如



The issue here is you are appending the same element over and over again on multiple clicks. You can remove the previously added element before adding a new one like

function Confirm() {

    var form = document.forms[0];

    // Remove the previous element added
    var oldInput = document.getElementById('myInput');
    if (oldInput !== null) form.removeChild(oldInput);

    // Add a new element
    var confirm_value = document.createElement("INPUT");
    confirm_value.setAttribute('id', 'myInput');
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";

    if (confirm("Do you want to delete supplier ?")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }

    form.appendChild(confirm_value);
}


在分配之前清除是/否值。

Clear the Yes/No value before assigning it.
function Confirm() {
    var confirm_value;
    
    confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    confirm_value.value = "";
    confirm_value.value = "";
    if (confirm("Do you want to delete supplier ?")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }
    
    document.forms[0].appendChild(confirm_value);
}





-KR



-KR


这篇关于如何在Java脚本中限制追加字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆