从呼叫复选框asp.net javascript函数 [英] call javascript function from asp.net checkbox

查看:133
本文介绍了从呼叫复选框asp.net javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jQuery函数如下,我想在一个复选框,点击调用。当我使用输入类型=复选框它工作正常。但是,我要检查/取消从C#code中的复选框(fillform()),并基于该javascript函数也有执行

I have a jQuery function as below which i want to be called on a checkbox click. when I use an input type="checkbox" it works fine. But I want to check/uncheck the checkbox from c# code(fillform()) and based on that the javascript function also has to execute

但如果我设置 =服务器或者如果我使用的asp:复选框那么下面的功能不叫。
我试着用ASP的下面的code:在Page_Load中复选框,以及输入框。但功能不叫

but if i set runat="server" or if i use asp:checkbox then the below function is not called. I tried using the below code for asp:checkbox as well as for input box in page_load. but the function is not called

C#code

Page_load
  {
    chkVehicle.Attributes.Add("onclick", "toggleVehicle();");

  } 
  FillForm
  {
    chkVehicle.Checked = ObjUR.HasVehicle;


  }

jQuery函数

jQuery function

 function toggleVehicle() {
        if ($('#chkVehicle').is(':checked')) {
            $('#divVehicle :input').removeAttr('disabled');
            $('#divVehicle').css({ "background-color": 'transparent' });
        } else {
            $('#divVehicle :input').attr('disabled', true);
            $('#divVehicle').css({ "background-color": 'gray' });
        }
    }

请帮忙

推荐答案

这是因为asp.net追加一些东西到IDS,像煤制油,等的ContentPlaceHolder ...像 ctl00_ContentPlaceHolder1_chkVehicle 。相反,你可以这样做:

That is because asp.net appends some more stuffs to the ids, like ctl, contentplaceholder etc... something like ctl00_ContentPlaceHolder1_chkVehicle. Instead you can do this:

 function toggleVehicle() {
         var chkVehicle  = '#<%= chkVehicle.ClientID %>';
        if ($(chkVehicle).is(':checked')) {
            $('#divVehicle :input').prop('disabled', false);
            $('#divVehicle').css({ "background-color": 'transparent' });
        } else {
            $('#divVehicle :input').prop('disabled', true);
            $('#divVehicle').css({ "background-color": 'gray' });
        }
    }

或只是指定一个类名和使用的className选择它。

or just assign a class name and select it using the className.

或使用jQuery属性 结束,与 选择,这可能因为虽然它做了通配符匹配转出高风险,慢也是如此。

or use jquery attribute ends-with selector, which could turn out risky though since it does a wildcard match, and slower as well.

      if ($('[id$="chkVehicle"]').is(':checked')) {
       ....

或暗示@jsonscript

or as @jsonscript suggests

chkVehicle.Attributes.Add("onclick", "toggleVehicle('#" + chkVehicle.ClientId + "');");

 function toggleVehicle(chkVehicle) {

        if ($(chkVehicle).is(':checked')) {
            $('#divVehicle :input').prop('disabled', false);
            $('#divVehicle').css({ "background-color": 'transparent' });
        } else {
            $('#divVehicle :input').prop('disabled', true);
            $('#divVehicle').css({ "background-color": 'gray' });
        }
    } 

此外,而不是使用的onclick 属性,处理程序绑定到的元素。

Also instead of using onclick attribute, bind the handler to the element.

  $(function(){ //DOM ready
       $('#<%= chkVehicle.ClientID %>').change(toggleVehicle);   

  });

   function toggleVehicle() {
         var $divVehicle = $('#divVehicle');
        if (this.checked) {
            $divVehicle.css({ "background-color": 'transparent' });
        } else {
            $divVehicle.css({ "background-color": 'gray' });
        }
        $divVehicle.find(':input').prop('disabled', !this.checked);
   }

此外,如果你不使用jQuery真的老版本的道具,而不是使用ATTR。这将更好地设置这些类型的属性比ATTR

这篇关于从呼叫复选框asp.net javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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