JavaScript-验证功能 [英] JavaScript - Validation Function

查看:75
本文介绍了JavaScript-验证功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证ASP.NET项目中的DropDownList,并且即使我以前从未使用过JavaScript,也尝试使用JavaScript.
我正在使用以下ASP声明和代码JavaScript:

ASP声明:

I need to validate a DropDownList in an ASP.NET project and am trying to use JavaScript even though I have never used it before.
I''m using the following ASP declaration and code JavaScript:

ASP declaration:

<asp:DropDownList ID="ddl1" runat="server" onprerender="ddl1_PreRender" ValidationGroup="AddNewCollection">
</asp:DropDownList>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Choose Type!"

ControlToValidate="ddl1"  ForeColor="Red" ValidationGroup="AddNewCollection" ClientValidationFunction="clientSideCheckValue"></asp:CustomValidator>>



JavaScript代码:



JavaScript code:

<script type="text/javascript">
    function clientSideCheckValue(source, args) 
     {
       var result1 = args.Value;   
       var rsult2 = document.getElementById("ddl1").value;
       if (result2 == null) {
          args.IsValid = false;
          return true;
       }
      args.IsValid = true;
    }
</script>



关于上述代码,我有两个问题:

1.为什么我在打印此行的代码时 var result1 = args.Value; intelisense不能给我选择"Value"扩展名的选项.

2.在这行JavaScript代码中, var rsult2 = document.getElementById("ddl1").value;

我收到此错误消息Microsoft JScript运行时错误:需要对象.您是否知道为什么遇到此问题以及如何解决?

预先谢谢您.



I have two questions about code mentioned above:

1.Why when I''m printing this row''s code var result1 = args.Value; intelisense dosen''t give me option to choose "Value" extension.

2.At this line of JavaScript code var rsult2 = document.getElementById("ddl1").value;

I get this error message Microsoft JScript runtime error: Object required.Have you got any idea why i meet this problem and how can i fix it?

Thank you in advance.

推荐答案

1.对于代码
var result1 = args.Value 

(不支持intelisense)
2.使用

(intelisense not supported)
2. use

var rsult2 = document.getElementById('<%=ddl1.ClientID %>').value;

代替

var rsult2 = document.getElementById("ddl1").value;




ddl不能这样工作.

使用以下代码在JS中访问ddl.

Hi,

ddl does not works like this.

use below code for access ddl in JS.

ddlObj = document.getElementById('<%=ddl1.ClientID %>');
ddlVal = ddlObj.options[ddlObj.selectedIndex].value;


1.为什么当我打印此行的代码时var result1 = args .价值; intelisense没有给我选择"Value"扩展名的选项.
此处 [
如果您使用的是用户控件,则将页面呈现到浏览器后,下拉列表的名称肯定会更改.尝试在浏览器上查看页面的来源,以验证下拉列表的名称是否确实发生了更改.
1.Why when I''m printing this row''s code var result1 = args.Value; intelisense dosen''t give me option to choose "Value" extension.
Here[^] is a nice explanation on your question.

2.At this line of JavaScript code var rsult2 = document.getElementById("ddl1").value;
If you are using user controls, the name of the dropdownlist will definitely change after your page has been rendered to the browser. Try to view the source of the page on your browser to verify the name of the dropdownlist if it has indeed changed.


这篇关于JavaScript-验证功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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