最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Validation (HTML5): Attribute 'X' is not a valid attribute of element 'Y' - Stack O

programmeradmin3浏览0评论

I quite often do this:

<input type="text" id="whatever" quantity="2" price="15" />

which causes VS2010 to issue the above warning -- I can live with the warnings because the inherent value in being able to hang data on form fields (which allows me later to pick them up for use in code e.g. $('#whatever').attr('price'), or simply document.getElementById('whatever').price) makes the nuisance warnings worth bearing. However, I was wondering what the canonical approach to this problem is...

anyone?

TIA - ekkis

I quite often do this:

<input type="text" id="whatever" quantity="2" price="15" />

which causes VS2010 to issue the above warning -- I can live with the warnings because the inherent value in being able to hang data on form fields (which allows me later to pick them up for use in code e.g. $('#whatever').attr('price'), or simply document.getElementById('whatever').price) makes the nuisance warnings worth bearing. However, I was wondering what the canonical approach to this problem is...

anyone?

TIA - ekkis

Share Improve this question asked Sep 21, 2011 at 20:20 ekkisekkis 10.3k13 gold badges61 silver badges113 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You should replace quantity="2" price="15" with data-quantity="2" data-price="15" -- which has the added bonus of making the values accessible through jQuery's .data() method:

alert( $('#whatever').data('price') ); // displays 15

http://blog.jquery./2011/05/03/jquery-16-released/

If you're using jQuery, you can store those variables in the object itself with .data():

$('#whatever').data('quantity', 2);

But I suggest splitting up your <input /> into multiple <input />s if you absolutely need to keep the information in the HTML file:

<input type="hidden" id="whatever-quantity" value="2" />
<input type="hidden" id="whatever-price" value="15" />

But for plete HTML5 awesomeness, just prepend data- to your attributes:

<input type="text" id="whatever" data-quantity="2" data-price="15" />
发布评论

评论列表(0)

  1. 暂无评论