开发者

Adding a jquery function to form inputs that have a dot in their ID

开发者 https://www.devze.com 2023-03-29 00:42 出处:网络
I am using a datepick() function to add a calendar to date inputs.This works fine UNLESS the id of the input is named with a dot.I need the dot since this is a very dynamic form and I will never know

I am using a datepick() function to add a calendar to date inputs. This works fine UNLESS the id of the input is named with a dot. I need the dot since this is a very dynamic form and I will never know the number of sessionStartDate(s) in the form.

What am I missing?

function addDatePick(){
$('#evntSes1.sessionStartDate').datepick();
$('#evntSes2.sessionStartDate').datepick();  
}
<select id="evntSes1.sessionTypeID" name="evntSes1.sessionTypeID">
<select id="ev开发者_如何转开发ntSes2.sessionTypeID" name="evntSes2.sessionTypeID">

Thanks for any suggestions


use two backslashes before dot:

function addDatePick(){
$('#evntSes1\\.sessionStartDate').datepick();
$('#evntSes2\\.sessionStartDate').datepick();  
}
<select id="evntSes1.sessionTypeID" name="evntSes1.sessionTypeID">
<select id="evntSes2.sessionTypeID" name="evntSes2.sessionTypeID">


because the dot syntax is used for classes. With your syntax, it will look for a div with the id "eventSes1" that has a class "sessionStartDate".

What you could do is in your form generator/html, use sessionsStartDate as a class, instead of appending it to the id with the dot. If you're being lazy and don't want to change your "counting" algo for your form, then just change the dot to a - or something


The . is a special character to jQuery for selecting classes. In your case it is looking for an element with id evntSes1 and the class sessionStartDate.

To get jQuery to recognize the . as part of the id, you need to escape it with a single backslash just in front of the dot. See official documentation on selectors for further information.

For completions sake there are other characters, too, that need escaping. These are !"#$%&'()*+,./:;<=>?@[]^`{|}~.

To make your case serve as an example, you'd escape like so:

$('#evntSes1\\.sessionStartDate')

Note that you need to type 2 backslashes as one is needed to escape the backslash within the JavaScript string. jQuery will receive a selector that looks like #evntSes1\.sessionStartDate from the example.

0

精彩评论

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