如果在Jquery中将枚举与字符串进行比较时不起作用。 [英] Required if doesn't working when comparing an enum with string in Jquery.

查看:238
本文介绍了如果在Jquery中将枚举与字符串进行比较时不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我需要你的帮助。我有一个RequiredIf,它比较了EnumDropdown和string字段中的值,但代码在Jquery验证中不起作用。任何人都可以建议一种方法来做到这一点。下面是代码片段。



ViewModel:



 [RequiredIf(  ValidationOption,CustomerParameter.Phone,ErrorMessageResourceType = < span class =code-keyword> typeof (Customer_cshtml),ErrorMessageResourceName =   PhoneRequired)] 
public string Phoner { get ; set ; }


[RequiredIf( ValidationOption,CustomerParameter。 Account,ErrorMessageResourceType = typeof (Customer_cshtml),ErrorMessageResourceName = CustomerRequired)]

public string 帐户{获得; set ; }

public CustomerParameter ValidationOption { get ; set ; }





JQUERY:



 $ .validator.addMethod('  requiredif'
function( value ,元素,参数){
var id = ' #' +参数[' dependentproperty'];


var targetvalue = parameters [' < span class =code-string> targetvalue'];
targetvalue =
(targetvalue == null ' ':targetvalue).toString();


var control = $(id);
var controltype = control.attr(' type'); ---> // 此行未定义为EnumDropdownList属性
var actualvalue;
if (controltype === ' 复选框'
{
actualvalue = control。 ' :checked')。toString();
} else if (controltype === ' radio'){
actualvalue = $(' input [name =' + parameters [' dependentproperty'] + ' ]:选中')。val()
} else {
actualvalue = control.val();
}


如果 targetvalue === actualvalue )---> /// ////在此比较区域中,如果获取电话或帐户等属性的名称,实际值是获取所选枚举值的值,如0,1等,因此规则不会被添加。
return $ .validator.methods.required.call(
this ,元素,参数);

return true ;
}
);

解决方案

.validator.addMethod( ' requiredif'
函数( value ,元素,参数){
var id = ' #' + parameters [' dependentproperty'];


< span class =code-keyword> var targetvalue = parameters [' targetvalue'] ;
targetvalue =
(targetvalue == null ' ':targetvalue).toString();


var control =


(id);
var controltype = control.attr(' type '); ---> // 此行未定义for EnumDropdownList属性
var actualvalue;
if (controltype === ' 复选框'
{
actualvalue = control。 ' :checked')。toString();
} else if (controltype === ' radio'){
actualvalue =


' input [name =' + parameters [' dependentproperty'] + ' ]:checked')。val()
} else {
actualvalue = control.val();
}


如果 targetvalue === actualvalue )---> /// ////在此比较区域中,如果获取电话或帐户等属性的名称,实际值是获取所选枚举值的值,如0,1等,因此规则不会被添加。
返回

Hello Members.

I need a slight help from you. I have a "RequiredIf" which is comparing a value from "EnumDropdown" and a "string" field but the code is not working in Jquery validation. Can anyone please suggest a way to do that.Below are the code snippets.

ViewModel :

[RequiredIf("ValidationOption", CustomerParameter.Phone, ErrorMessageResourceType = typeof(Customer_cshtml), ErrorMessageResourceName = "PhoneRequired")]
        public string Phoner { get; set; }


[RequiredIf("ValidationOption", CustomerParameter.Account, ErrorMessageResourceType = typeof(Customer_cshtml), ErrorMessageResourceName = "CustomerRequired")]
       
        public string Account{ get; set; }

public CustomerParameter ValidationOption { get; set; }



JQUERY:

$.validator.addMethod('requiredif',
    function (value, element, parameters) {
        var id = '#' + parameters['dependentproperty'];

        
        var targetvalue = parameters['targetvalue'];
        targetvalue =
          (targetvalue == null ? '' : targetvalue).toString();

        
        var control = $(id);
        var controltype = control.attr('type'); --->//This line is getting undefined for EnumDropdownList property
        var actualvalue ;
        if (controltype === 'checkbox')
        {
            actualvalue = control.is(':checked').toString();
        } else if (controltype === 'radio') {
            actualvalue = $('input[name='+parameters['dependentproperty']+']:checked').val()
        } else {
            actualvalue = control.val();
        }

       
            if (targetvalue === actualvalue)--->///////In this comparison area targetvalue if fetching the name of the property like Phone or Account and the actual value is fetching the value of selected enums value like 0,1 etc so the rules are not getting added.
            return $.validator.methods.required.call(
              this, value, element, parameters);

        return true;
    }
);

解决方案

.validator.addMethod('requiredif', function (value, element, parameters) { var id = '#' + parameters['dependentproperty']; var targetvalue = parameters['targetvalue']; targetvalue = (targetvalue == null ? '' : targetvalue).toString(); var control =


(id); var controltype = control.attr('type'); --->//This line is getting undefined for EnumDropdownList property var actualvalue ; if (controltype === 'checkbox') { actualvalue = control.is(':checked').toString(); } else if (controltype === 'radio') { actualvalue =


('input[name='+parameters['dependentproperty']+']:checked').val() } else { actualvalue = control.val(); } if (targetvalue === actualvalue)--->///////In this comparison area targetvalue if fetching the name of the property like Phone or Account and the actual value is fetching the value of selected enums value like 0,1 etc so the rules are not getting added. return


这篇关于如果在Jquery中将枚举与字符串进行比较时不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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