简体   繁体   English

当 onClientClick 返回 true 时,OnClick 事件不会触发

[英]OnClick event not firing when onClientClick returns true

i have a button with both onclientclick and onclick events.我有一个带有 onclientclick 和 onclick 事件的按钮。 I also have a jquery in my page.我的页面中也有一个 jquery。 This jquery registers the buttons click event when timer elpase.这个jquery在定时器elpase时注册按钮点击事件。 The OnclientClick calls a function that validates whether a radio button is clicked or not...when timer is running...if user clicks the (Next) button without selecting an option(radiobuttonList)...it returns false and hence the server side event is prevented.. However, if timer elapse and no item is selected, the form is submitted with blank options here is the code, OnclientClick 调用一个函数来验证是否单击了单选按钮......当计时器正在运行时......如果用户点击(下一步)按钮而不选择选项(radiobuttonList)......它返回false,因此服务器副事件被阻止.. 但是,如果计时器过去并且没有选择任何项目,则提交表单时带有空白选项,这里是代码,

var countdownTimer, countdownCurrent;
$(document).ready(function() {
    countdownCurrent = $('#ctl00_MainContent_example2submit').val() * 100;
    countdownTimer = $.timer(function () {
    var min = parseInt(countdownCurrent / 6000);
    var sec = parseInt(countdownCurrent / 100) - (min * 60);
    var micro = pad(countdownCurrent - (sec * 100) - (min * 6000), 2);
    var output = "00"; if (min > 0) { output = pad(min, 2); }
    $('.countdowntime').html(output + ":" + pad(sec, 2) + ":" + micro);
    if (countdownCurrent == 0) {
        $('#ctl00_MainContent_btnNext').click();

    } else {
        countdownCurrent -= 7;
        if (countdownCurrent < 0) { 
            countdownCurrent = 0; 
        }
    }
}, 70, true);
$('#example2submit').bind('keyup', function (e) { 
    if (e.keyCode == 13) { 
        countdownReset(); 
    } 
});

function CheckIfOptionSelected() {
    var vFlag = true;
    var radioButton1 = document.forms[0].elements['ctl00_MainContent_rdBtnListOptions_0'];
    var radioButton2 = document.forms[0].elements['ctl00_MainContent_rdBtnListOptions_1'];
    var radioButton3 = document.forms[0].elements['ctl00_MainContent_rdBtnListOptions_2'];
    var radioButton4 = document.forms[0].elements['ctl00_MainContent_rdBtnListOptions_3'];

    if (radioButton1.checked == false && radioButton2.checked == false && radioButton3.checked == false && radioButton4.checked == false && countdownCurrent > 0) {
        vFlag = false;
    }

    else {
        countdownReset();
        vFlag = true;

    }
    return vFlag;
}

function countdownReset() {
    var newCount = parseInt($('#ctl00_MainContent_example2submit').val()) * 100;
    if (newCount > 0) { countdownCurrent = newCount; }
}

The Problem i'm facing is, If i allow the timer to elapse with nothing selected in the radiobutton list...the Onclick event fires,a successful postback, but when i select an option...and without clicking the NEXT button i allow the timer to elapse....the postback does not occur??.... The Next button is as follows:我面临的问题是,如果我允许计时器在单选按钮列表中没有选择任何内容的情况下结束...... Onclick 事件触发,成功回发,但是当我选择一个选项......并且没有点击 NEXT 按钮时,我允许定时器超时....回发不发生??....下一步按钮如下:

<asp:Button ID="btnNext" runat="server" Height="26px" 
            OnClientClick="if(!CheckIfOptionSelected()){return false;};" 
            OnClick="btnNext_Click" Text="Next"
            Width="77px" CausesValidation="False" />

Try like this:像这样尝试:

OnClientClick="return CheckIfOptionSelected();" UseSubmitBehavior="false"

If the function returns true , onclick will definitely work!如果函数返回true , onclick 肯定会起作用!

if we use UseSubmitBehavior="false", it is not working.No need to give CausesValidation="False" also.如果我们使用 UseSubmitBehavior="false",它就不起作用。也不需要给出 CausesValidation="False"。

sample java script function below to check for null value when the user clicks submit button without entering :下面的示例 java 脚本函数用于在用户单击提交按钮而不输入时检查空值:

    function validatePwd()
      {
        var txtpwd = document.getElementById('txtpwd').value;
        if (txtpwd == "") {
            alert("Please enter new  password");
            return false;
        }
        else {
            return true;
        }

    } 

just below events are enough in the asp.net button control在 asp.net 按钮控件中,仅在事件下方就足够了

OnClientClick="return validatePwd();" 

OnClick="btnsubmit_Click"  

OnClientClick="return CheckIfOptionSelected(); return true;" is working perfectly fine for me.对我来说工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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