动态更改 HTML 元素的类无效
我编写了一个包含表单的 HTML 页面。 PHP 页面通过在 HTML 页面开头检查表单:
<script type="text/javascript" src="js/mootools/core.js"></script>
<script type="text/javascript" src="js/mootools/more.js"></script>
<script type="text/javascript" src="js/formcheck/lang/fr.js"> </script>
<script type="text/javascript" src="js/formcheck/formcheck.js"> </script>
<script type="text/javascript">
window.addEvent('domready', function(){
new FormCheck('form');
});
</script>
问题是在某些条件下加载页面后,我动态地将类添加到 HTML 元素。
以下 HTML 页面的代码示例是一个单选按钮,用于了解页面的访问者是否有汽车。如果她/他选择“是”,则添加“验证['必需']”类来选择汽车的元素数量,这意味着用户必须选择一个值(汽车数量)。否则,用户没有汽车,则不必选择,并且接受并传输默认的空字符串。
<input type="radio" name="cars" value="yes" onclick="document.getElementById('number_of_cars').setAttribute('class','validate[\'required\']');">oui
<input type="radio" name="cars" value="no" checked="checked">no
<label>How many cars do you have ? :</label>
<select name="number_of_cars" id="number_of_cars">
<option value="">-</option>
<option value="01" >01</option>
<option value="02" >02</option>
<option value="03" >03</option>
</select>
我遇到的问题是,当我使用 Firebug 检查时,HTML 元素在需要时具有类“validate['required']”(单选按钮是),但 PHP 页面似乎不起作用,因为即使表单被传输,用户尚未选择多辆车。
我的猜测是,我的 HTML 页面的 Javascript 部分上的事件“domready”不合适,但我不知道该怎么办。
编辑:我已将 javascript 元素更改为 onChange 但它也不起作用。
I've written an HTML page that contains a form. The form is checked by a PHP page by having at the beginning of the HTML page :
<script type="text/javascript" src="js/mootools/core.js"></script>
<script type="text/javascript" src="js/mootools/more.js"></script>
<script type="text/javascript" src="js/formcheck/lang/fr.js"> </script>
<script type="text/javascript" src="js/formcheck/formcheck.js"> </script>
<script type="text/javascript">
window.addEvent('domready', function(){
new FormCheck('form');
});
</script>
The problem is that I add dynamically a class to an HTML element after the page is loaded under some conditions.
The following code sample of the HTML page is a radio button to know if the visitor of the page has a car. If she/he puts yes then the class 'validate['required']' is added to select element number of cars meaning the user must choose a value (number of cars). Otherwise the user has no car and then doesn't have to choose and the default empty string is accepted and transmitted.
<input type="radio" name="cars" value="yes" onclick="document.getElementById('number_of_cars').setAttribute('class','validate[\'required\']');">oui
<input type="radio" name="cars" value="no" checked="checked">no
<label>How many cars do you have ? :</label>
<select name="number_of_cars" id="number_of_cars">
<option value="">-</option>
<option value="01" >01</option>
<option value="02" >02</option>
<option value="03" >03</option>
</select>
The problem I have is that when I check with Firebug the HTML element has the class 'validate['required']' when needed (radio button yes) but the PHP page doesn't seem to work because the form is transmitted even if the user hasn't choosed a number of cars.
My guess is that the event "domready" on the Javascript part of my HTML page is not appropriate but I don't know what to do.
Edit: I've changed the javascript element to onChange but it doesn't work neither.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
window.addEvent('domready') 是 MooTools 库的一部分,它不是 Javascript 本身的一部分。在我看来,你没有使用它,所以你应该将 MooTools 脚本添加到你的 html 中,或者使用 onLoad 事件。MooTools 使用
编辑
要么是那个,要么是值您使用 FormCheck 对象发送的是错误的。 FormCheck 需要一个 id 而不是标记名,因此您至少应该在某个地方:
编辑 2
如果您已经在使用 MooTools,则最好使用内置事件处理程序。由于跨浏览器问题,应避免使用 onClick 和 onLoad 等直接方法。 MooTools 可以解决这个问题。
MooTools 需要 id 才能正常工作,因此:
编辑 3
另请仔细阅读 FormCheck 的文档。它们包含大量示例代码: http://mootools.floor.ch/en/demos/formcheck /
文档:http://mootools.floor.ch/docs/formcheck/files/formcheck-js.html
希望这有帮助
The window.addEvent('domready') is part of the MooTools library, it is not part of Javascript itself. It seems to me that you are not using that, so you should either add the MooTools script to your html, or use the onLoad event.MooTools is used
Edit
Either that, or the value you send with the FormCheck object is wrong. FormCheck needs an id and not a tagname, so you should at least have somewhere:
Edit 2
If you're already using MooTools, it's best to use the built-in event handlers. Direct approaches like onClick and onLoad should be avoided because of cross-browser issues. MooTools takes care of that.
MooTools needs id's for this to work, so:
Edit 3
Also read the docs of FormCheck carefully. They contain lots of sample code: http://mootools.floor.ch/en/demos/formcheck/
Docs: http://mootools.floor.ch/docs/formcheck/files/formcheck-js.html
Hope this helps