【问题标题】:JScript Button working in chrome but not in FirefoxJScript Button 在 chrome 中工作,但在 Firefox 中不工作
【发布时间】:2014-08-01 17:14:43
【问题描述】:

我有一个脚本,它基本上在单击按钮时附加属性。脚本如下;

<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>

在我的 HTML 中,我使用普通的 onClick 函数来回调函数

<div id="add"></div>
<p><input type="button" value="Add" name="" onclick="generateRow()"/></p>

我遇到的问题是在我的 Chrome 版本中,我可以在每次单击按钮时添加一个字段,但在 Firefox 和 Chrome 的更新版本中,该按钮不会响应任何内容。

我使用的是 Firefox 31.0 版,我使用的是 Chrome 32.0 版

【问题讨论】:

    标签: javascript html google-chrome firefox


    【解决方案1】:

    NodeElement 方法setAttribute 需要两个参数,但input.setAttribute('required') 行只有一个。您必须改用input.setAttribute('required', true);

    更新后你的整个代码:

    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);
    }
    

    DEMO

    【讨论】:

    • 哇,我从没想过会是这样,我在这里挠了几天。非常感谢它的工作:)
    【解决方案2】:

    http://jsfiddle.net/ZZ7T5/2/ 它指向小提琴问题的链接在 setAttribute("required") 中。它要求两个参数,而您只传递一个参数

    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);
    }
    

    【讨论】:

    • 感谢您的回复,我有 2 个相同且正确的类似答案,因此我必须公平并接受第一个回答的人。再次感谢。
    • @RuzrollerAndhar 很好,它有帮助,并且查询已解决,这才是最重要的朋友.. :)
    猜你喜欢
    • 2014-11-30
    • 2016-04-18
    • 2017-05-10
    • 2021-08-12
    • 2016-07-19
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多