开发者

Illegal character when trying to escape square brackets with jquery

Using the jquery form plugin. I was trying 开发者_如何学JAVAto test the value of a form element that uses square brackets.

            var form = jqForm[0];
            if (!form.sendTo\\[\\].value) {                
              alert('Something'); 
              return false; 
            }

Despite trying to escape the brackets it is still giving me a an error.

illegal character

[Break On This Error] if (!form.sendTo\[\].value) {

is it not possible to do this?

Thanks


you can use a square brackets and a string in place of dot-notation in javascript. So try this:

var form = jqForm[0];
if (!form["sendTo[]"].value) {                
   alert('Something'); 
   return false; 
}

Edit after comments:

Using jQuery you can determine how many checked checkboxes there are using code such as:

$('input[name="sendTo[]"]:checked',form).length

assuming form is a jQuery object referencing the form containing the checkboxes. if it's not, you should be able to wrap it like $(form) in the example above.

Live example: http://jsfiddle.net/TMSsq/


Use it as an associate array. Try this

        var form = jqForm[0];
        if (!form.["sendTo[]"].value) {                
          alert('Something'); 
          return false; 
        }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜