单击按钮执行3个javascript函数

时间:2014-10-07 06:09:40

标签: javascript asp.net

我正在使用3个javascript函数进行文本字段验证,

  1. 在正确输入所有字段后验证所有字段
  2. 是/取消确认也应该与a一起 弹出窗口,其中包含显示在页面中的文本填充值,以验证输入的值是否正确 一旦我选择是,我的按钮点击事件应该发生,如果没有选择什么sholud发生。弹出窗口可由用户关闭
  3. 这是我的js脚本,

    我将验证所有文本字段并获取确认框是/取消但弹出窗口页面未显示

    <script type="text/javascript">
    
    function UserDetailsConfirmation() {
        return confirm("Are you sure you want to add this ?");
        return openwin();
    }
    
    function openwin() {
       window.open('PopupForConfirm.aspx?name1=' + name + '&age1=' + age + '&gen=' + gender + '', 'open_window', ' width=640, height=480, left=0, top=0');
    }
    function validate() {
    
        var name= document.getElementById('<%=this.txtname.ClientID %>');
        var age= document.getElementById('<%=this.txtage.ClientID %>');
        var gender= document.getElementById('<%=this.txtgender.ClientID %>');
    
        if (name.value) == "") {
            alert("Please Enter Name");
            name.focus();
            return false;
        }
        else if (age.value) == "") {
            alert("Please Enter age");
            age.focus();
            return false;
        }
        else if (gender.value) == "") {
        alert("Please enter gender");
        gender.focus();
        return false;
        }
    
    else if (!UserDetailsConfirmation()) {
        return false;
        }
        else {
            return true;
        }
    }
    </script>
    

3 个答案:

答案 0 :(得分:0)

您在致电openwin之前就回来了。怎么可以达成?怎么样:

function UserDetailsConfirmation() {
  return confirm("Are you sure you want to add this ?") && openwin();
}

顺便说一句:

if (!UserDetailsConfirmation()) {
  return false;
}
else {
  return true;
}

归结为:

return UserDetailsConfirmation();

答案 1 :(得分:0)

<script type="text/javascript">

function UserDetailsConfirmation() {
    var r = confirm("Are you sure you want to add this ?");
    if (r == true) {
       openwin();
    } else {
    return false;
    }
  }

function openwin() {
   window.open('PopupForConfirm.aspx?name1=' + name + '&age1=' + age + '&gen=' + gender + '', 'open_window', ' width=640, height=480, left=0, top=0');
}
function validate() {

    var name= document.getElementById('<%=this.txtname.ClientID %>');
    var age= document.getElementById('<%=this.txtage.ClientID %>');
    var gender= document.getElementById('<%=this.txtgender.ClientID %>');

    if (name.value) == "") {
        alert("Please Enter Name");
        name.focus();
        return false;
    }
    else if (age.value) == "") {
        alert("Please Enter age");
        age.focus();
        return false;
    }
    else if (gender.value) == "") {
    alert("Please enter gender");
    gender.focus();
    return false;
    }

else if (!UserDetailsConfirmation()) {
    return false;
    }
    else {
        return true;
    }
}
</script>

我已经改变了你的代码......虽然不需要打开弹出窗口 我认为这已经足够了

答案 2 :(得分:0)

在您的onclick上尝试此操作,调用UserDetailsConfirmation:

<script type="text/javascript">

function UserDetailsConfirmation() {
    if(validate() == true)
    {
        var r = confirm("Are you sure you want to add this ?");
        if (r == true) {
            openwin();
        }
    } else {
        alert('Something went wrong');
    }
  }

function openwin() {
   window.open('PopupForConfirm.aspx?name1=' + name + '&age1=' + age + '&gen=' + gender + '', 'open_window', ' width=640, height=480, left=0, top=0');
}
function validate() {

    var name= document.getElementById('<%=this.txtname.ClientID %>');
    var age= document.getElementById('<%=this.txtage.ClientID %>');
    var gender= document.getElementById('<%=this.txtgender.ClientID %>');

    if (name.value) == "") {
        alert("Please Enter Name");
        name.focus();
        return false;
    }
    else if (age.value) == "") {
        alert("Please Enter age");
        age.focus();
        return false;
    }
    else if (gender.value) == "") {
        alert("Please enter gender");
        gender.focus();
        return false;
    }
    else{
        return true;
    }
}
</script>

已修改选项2

这实际上是基于您的代码..

<script type="text/javascript">

function UserDetailsConfirmation() {
    return confirm("Are you sure you want to add this ?");
}

function openwin() {
   window.open('PopupForConfirm.aspx?name1=' + name + '&age1=' + age + '&gen=' + gender + '', 'open_window', ' width=640, height=480, left=0, top=0');
}
function validate() {

    var name= document.getElementById('<%=this.txtname.ClientID %>');
    var age= document.getElementById('<%=this.txtage.ClientID %>');
    var gender= document.getElementById('<%=this.txtgender.ClientID %>');

    if (name.value) == "") {
        alert("Please Enter Name");
        name.focus();
        return false;
    }
    else if (age.value) == "") {
        alert("Please Enter age");
        age.focus();
        return false;
    }
    else if (gender.value) == "") {
        alert("Please enter gender");
        gender.focus();
        return false;
    }
    else if (UserDetailsConfirmation() == true) {
        openwin();
    }
    else{
        return false;
    }

}
</script>