8

我的问题很简单

$('#button').removeAttr('type');

在 firebug 上触发错误

type property can't be changed

我有两个问题:

  • 如何解决这个问题?
  • 是否有无法更改的属性列表的参考?

谢谢

编辑

我想做的是:

阻止按钮提交表单。

注意:我的按钮是作为 html 模板包含的,我无法访问表单代码。
就像是:

include 'form.php';
include 'buttons.php';

我只能控制buttons.php

4

3 回答 3

17

插入 DOM 后,您无法更改 IE (<9?) 中的input's 。type因此,jQuery 会为所有浏览器引发错误。

从来源:

    set: function( elem, value ) {
            // We can't allow the type property to be changed (since it causes problems in IE)
            if ( rtype.test( elem.nodeName ) && elem.parentNode ) {
                jQuery.error( "type property can't be changed" );

我不知道(或 jQuery 知道)其他情况,如何解决这个问题在很大程度上取决于您首先要尝试做什么。例如,考虑创建一个具有不同类型的新元素并使用它。

编辑:要防止按钮提交表单,您可以使用:

$("#button").click(function(e){
    e.preventDefault();
});

或者(信用 Tim Down):

$("#button").prop("disabled", true);

为了防止通过任何方式提交表单,您可以使用:

$("#form").submit(function(e){
    e.preventDefault();
});
于 2012-06-14T11:19:39.593 回答
6

你可以替换它:

$('#button').replaceWith('<input type="text" value="text"/>');

或使用event.preventDefault()

于 2012-06-14T11:27:50.790 回答
1

由于您只想禁用提交按钮:

如果您使用的是 jQuery < 1.6,请执行以下操作:

$("#button").attr("disabled", 'disabled');

如果您使用的是 jQuery 1.6+:

$("#button").prop("disabled", true);

请参阅此问题:.prop() vs .attr() 以获取参考原因。

于 2012-06-14T11:33:51.127 回答