奇怪的jQuery文本框问题

时间:2017-05-26 15:00:30

标签: javascript jquery html asp.net vb.net

我不能为我的生活把这一点弄清楚。我有两个javascript函数(jQuery)链接到使用后面的vb.net代码的按钮,如:

Button_NewProgram.Attributes.Add("OnClick", "return NewProgramEdits();")
Button_SaveSchedule.Attributes.Add("OnClick", "return ScheduleEdits()")

这两个按钮都是包含母版页的内容页面的一部分,内容中没有正文或表单标记,只是没有设置id的表格。两个javascript函数几乎都是准确的,但由于某种原因执行非常不同。我知道他们开火是因为我看到警报,我使用的是IE v11。我没有错误。功能是:

我为document.ready()

中的所有文本框设置了一个掩码
$("#<%=Inputmask_RegFee1.ClientID%>").inputmask("999");
$("#<%=Inputmask_RegFee2.ClientID%>").inputmask("999");
$("#<%=Inputmask_RegFee3.ClientID%>").inputmask("999");
$("#<%=Inputmask_RegFee4.ClientID%>").inputmask("999");
$("#<%=Inputmask_RegFee5.ClientID%>").inputmask("999");
$("#<%=Inputmask_RegFee6.ClientID%>").inputmask("999");
$("#<%=Inputmask_RegFee7.ClientID%>").inputmask("999");
$("#<%=Inputmask_RegFee8.ClientID%>").inputmask("999");
$("#<%=Inputmask_RegFee9.ClientID%>").inputmask("999");
$("#<%=Inputmask_RegFee10.ClientID%>").inputmask("999");    
$("#<%=Inputmask_Fee1.ClientID%>").inputmask("999");
$("#<%=Inputmask_Fee2.ClientID%>").inputmask("999");
$("#<%=Inputmask_Fee3.ClientID%>").inputmask("999");
$("#<%=Inputmask_Fee4.ClientID%>").inputmask("999");
$("#<%=Inputmask_Fee5.ClientID%>").inputmask("999");
$("#<%=Inputmask_Fee6.ClientID%>").inputmask("999");
$("#<%=Inputmask_Fee7.ClientID%>").inputmask("999");
$("#<%=Inputmask_Fee8.ClientID%>").inputmask("999");
$("#<%=Inputmask_Fee9.ClientID%>").inputmask("999");
$("#<%=Inputmask_Fee10.ClientID%>").inputmask("999");

' This one works, yet the alert shows nothing for the value of fee1 
' and programname yet returns the correct response as all the fees
' are zero and the program IS set to a value ("test').  Strange.

function NewProgramEdits() {
    var fee1 = $('#<%=Inputmask_Fee1.ClientID%>').val().replace(/_/g, "");
    alert("New Program Fee 1 = ") + fee1;
    var fee2 = $('#<%=Inputmask_Fee2.ClientID%>').val().replace(/_/g,"");
    var fee3 = $('#<%=Inputmask_Fee3.ClientID%>').val().replace(/_/g,"");
    var fee4 = $('#<%=Inputmask_Fee4.ClientID%>').val().replace(/_/g,"");
    var fee5 = $('#<%=Inputmask_Fee5.ClientID%>').val().replace(/_/g,"");
    var fee6 = $('#<%=Inputmask_Fee6.ClientID%>').val().replace(/_/g,"");
    var fee7 = $('#<%=Inputmask_Fee7.ClientID%>').val().replace(/_/g,"");
    var fee8 = $('#<%=Inputmask_Fee8.ClientID%>').val().replace(/_/g,"");
    var fee9 = $('#<%=Inputmask_Fee9.ClientID%>').val().replace(/_/g,"");
    var fee10 = $('#<%=Inputmask_Fee10.ClientID%>').val().replace(/_/g,"");
    var newprogram = $('#<%=TextBox_NewProgram.ClientID%>').val().toUpperCase();
    alert("Program Name = ") + newprogram;

    if (newprogram == '') {
        alert('Enter the program name.');
        $('#<%=TextBox_NewProgram.ClientID%>').focus();
       return false;
    } else if (fee1 == '0' && fee2 == '0' && fee3 == '0' && fee4 == '0' && fee5 == '0' && fee6 == '0' && fee7 == '0' && fee8 == '0' && fee9 == '0' && fee10 == '0') {
        alert('You must enter at least one fee value for a new program, they cannot all be zero.');
        $('#<%=Inputmask_Fee1.ClientID%>').focus();
        return false;
    } else {
        return true;
    }
}

出于某种原因,这个不起作用:

    ' The alerts show nothing or empty string even though 
    ' the default values for the textboxes is ALWAYS zero, 
    ' the replace code replaces the mask character with an empty string
    ' the actual value of ALL the textboxes is "0__" (two underscores)

function ScheduleEdits() {
    var regfee1 = $('#<%=Inputmask_RegFee1.ClientID%>').val().replace(/_/g, "");
    var regfee2 = $('#<%=Inputmask_RegFee2.ClientID%>').val().replace(/_/g, "");
    var regfee3 = $('#<%=Inputmask_RegFee3.ClientID%>').val().replace(/_/g, "");
    var regfee4 = $('#<%=Inputmask_RegFee4.ClientID%>').val().replace(/_/g, "");
    var regfee5 = $('#<%=Inputmask_RegFee5.ClientID%>').val().replace(/_/g, "");
    var regfee6 = $('#<%=Inputmask_RegFee6.ClientID%>').val().replace(/_/g, "");
    var regfee7 = $('#<%=Inputmask_RegFee7.ClientID%>').val().replace(/_/g, "");
    var regfee8 = $('#<%=Inputmask_RegFee8.ClientID%>').val().replace(/_/g, "");
    var regfee9 = $('#<%=Inputmask_RegFee9.ClientID%>').val().replace(/_/g, "");
    var regfee10 = $('#<%=Inputmask_RegFee10.ClientID%>').val().replace(/_/g, "");
    var programtype = $('#<%=RadioButtonList_RegFeeMethod.ClientID%>').val().toUpperCase();

    alert("Fee 1 = ") + regfee1;
    alert("Program Type = ") + programtype;

    if (programtype == 'SCHEDULE' && regfee1 == '0' && regfee2 == '0' && regfee3 == '0' && regfee4 == '0' && regfee5 == '0' && regfee6 == '0' && regfee7 == '0' && regfee8 == '0' && regfee9 == '0' && regfee10 == '0') {
        alert('You must enter at lease one fee value for the schedule program.');
        $('#<%=Inputmask_RegFee1.ClientID%>').focus();
        return false;
    } else {
        return true;
    }
}

注意:为radiobuttonlist添加了标记

<asp:RadioButtonList ID="RadioButtonList_RegFeeMethod" runat="server" RepeatDirection="Horizontal">
    <asp:ListItem Value="SCHEDULE">Schedule</asp:ListItem>
    <asp:ListItem Value="PROGRAM">Program</asp:ListItem>
    <asp:ListItem Value="ZEROFEE">Zero Fee</asp:ListItem>
</asp:RadioButtonList>

2 个答案:

答案 0 :(得分:2)

  {"coupon":{"amount":"149","endDate":"04/12/2020","id":"6","message":"only big sizes","price":"79.9","startDate":"07/08/2014","title":"pajamas","type":"FASHION"}}

答案 1 :(得分:2)

考虑以下代码:

<asp:RadioButtonList ID="RadioButtonList_RegFeeMethod" runat="server" RepeatDirection="Horizontal">
    <asp:ListItem Value="SCHEDULE">Schedule</asp:ListItem>
    <asp:ListItem Value="PROGRAM">Program</asp:ListItem>
    <asp:ListItem Value="ZEROFEE">Zero Fee</asp:ListItem>
</asp:RadioButtonList>

要获取所选无线电检查的值,您需要使用$("#ID input:checked")来捕获正确的元素。这看起来像是:

var programtype = $("#<%=RadioButtonList_RegFeeMethod.ClientID%> input:checked").val();

这应该选择正确的<input>元素,并允许您访问它的value属性。