开发者

jQuery selector with ASP.NET controls

开发者 https://www.devze.com 2023-01-30 01:56 出处:网络
iam using ajax in asp.net page for state, city and zip code field. i want this ajax functionality to be generic. i want to use the same ajax functionality for the textbox and label controls with diffe

iam using ajax in asp.net page for state, city and zip code field. i want this ajax functionality to be generic. i want to use the same ajax functionality for the textbox and label controls with different ids. i tried with cssclassproperty but it applied to both set of controls. the code is following below

<script type="text/javascript">
   $(document).ready(function () {

       $('.csszipcode').blur(function (event) {

           var text = $('.csszipcode').val();

           if (text == "") {


               $('.csslblcity').text("");
               $('.csslblstate').text("");
               return;
           }

           var isValid = /^[0-9]{5}(?:-[0-9]{4})?$/.test(text);

           if (!isValid) {
               $('.csslblcity').text("");
               $('.csslblstate').text("");


               return;
           }


           $.ajax({

               type: "POST",
               url: "/SERVICES/DataService.asmx/getCityState",
               data: "{'zipcode': '" + $('.csszipcode').val() + "'}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function (msg) {
                   AjaxSucceeded(msg);
               },
               error: AjaxFailed

           });

       });

   });

   function AjaxSucceeded(result) {
       var city = result.d.split(':')[0];
       var state = result.d.split(':')[1];

       if (city != null && state != null) {

           $('.csslblcity').html(city);
           $('.csslblstate').html(state开发者_运维问答);
       }
       else {
           $('.csslblcity').text("");
           $('.csslblstate').text("");

       }
   }

   function AjaxFailed(result) {
       alert(result.status + ' ' + result.statusText);
   }

</script>

the html codes are following. i want to apply the ajax functionality for the following two textboxes. how can i differentiate which is currently active.

<asp:textbox id="txtzipcode" runat="server" cssclass="csszipcode"></asp:textbox>
<asp:textbox id="txtcandacode" runat="server" cssclass="csszipcode"></asp:textbox>


I'm not entirely clear what you are asking.

You say:

i want to use the same ajax functionality for the textbox and label controls with different ids.

Implying that you want the same functionality for textboxes and labels, but then say:

i tried with cssclassproperty but it applied to both set of controls. the code is following below

Implying that you don't want it to apply to textboes and labels.

Can you clarify?

Also, post your HTML (preferably the rendered code). This will help us ascertain how best to achieve what you want.

I can take a stab at an answer but given that I'm not sure what you are asking, may probably be wrong.

ASP.NET label controls are rendered as <span> HTML elements. To differentiate between textboxes and spans with the same class, use the following selectors

$('input.csszipcode')
$('span.csszipcode')

To narrow the selector to textboxes only, try this:

$('input:text.csszipcode')

If this isn't what you are after can you add more clarification?

0

精彩评论

暂无评论...
验证码 换一张
取 消