[英]JScript Button working in chrome but not in Firefox
I have a script which basically appends attributes when a button is clicked.我有一个脚本,它基本上在单击按钮时附加属性。 Script is as follows;
脚本如下;
<script type="text/javascript">
window.generateRow = function() {
var d = document.getElementById("add");
var p = document.createElement("p");
var input = document.createElement("input");
input.setAttribute('type', 'text');
input.setAttribute('pattern', '^[a-zA-Z ]+$');
input.setAttribute('name', 'items[]');
input.setAttribute('required');
p.appendChild(input);
d.appendChild(p);
}
</script>
And in my HTML I use a normal onClick function to call back the function在我的 HTML 中,我使用一个普通的 onClick 函数来回调函数
<div id="add"></div>
<p><input type="button" value="Add" name="" onclick="generateRow()"/></p>
The problem I am having is in my version of Chrome I am able to add a field each time the button is clicked, but in Firefox and a updated version of Chrome the button does not respond to anything.我遇到的问题是在我的 Chrome 版本中,每次单击按钮时我都可以添加一个字段,但是在 Firefox 和 Chrome 的更新版本中,该按钮没有任何响应。
I am using Firefox version 31.0 and i am using Chrome version 32.0我使用的是 Firefox 31.0 版,我使用的是 Chrome 32.0 版
NodeElement
method setAttribute
requires two arguments, but line input.setAttribute('required')
has only one. NodeElement
方法setAttribute
需要两个参数,但行input.setAttribute('required')
只有一个。 You have to use input.setAttribute('required', true);
你必须使用
input.setAttribute('required', true);
instead.反而。
Your whole code after update:更新后的整个代码:
generateRow = function() {
alert('m');
var d = document.getElementById("add");
var p = document.createElement("p");
var input = document.createElement("input");
input.setAttribute('type', 'text');
input.setAttribute('pattern', '^[a-zA-Z ]+$');
input.setAttribute('name', 'items[]');
input.setAttribute('required', true);
p.appendChild(input);
d.appendChild(p);
}
http://jsfiddle.net/ZZ7T5/2/ its link to fiddle problem is in setAttribute("required"). http://jsfiddle.net/ZZ7T5/2/其小提琴问题的链接在 setAttribute("required") 中。 Its asking for two params while you are only passing one
它要求两个参数,而您只传递一个参数
window.generateRow = function() {
var d = document.getElementById("add");
var p = document.createElement("p");
var input = document.createElement("input");
input.setAttribute('type', 'text');
input.setAttribute('pattern', '^[a-zA-Z ]+$');
input.setAttribute('name', 'items[]');
input.setAttribute('required', true); // THIS IS ERR
p.appendChild(input);
d.appendChild(p);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.