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

javascript - jQuery removeAttr('type') doesn't work - Stack Overflow

programmeradmin4浏览0评论

My problem is very simple

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

triggers an error on firebug

type property can't be changed

I have 2 questions:

  • How to get around this?
  • Is there a reference with a list of properties that can't be changed?

Thanks

EDIT

What I want to do is:

Prevent the button from submitting the form.

Note: my button is included as a html template, the form code is not accessible form me.
Something like:

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

where I have control only on buttons.php

My problem is very simple

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

triggers an error on firebug

type property can't be changed

I have 2 questions:

  • How to get around this?
  • Is there a reference with a list of properties that can't be changed?

Thanks

EDIT

What I want to do is:

Prevent the button from submitting the form.

Note: my button is included as a html template, the form code is not accessible form me.
Something like:

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

where I have control only on buttons.php

Share Improve this question edited Jun 14, 2012 at 11:25 ilyes kooli asked Jun 14, 2012 at 11:18 ilyes kooliilyes kooli 12k14 gold badges54 silver badges81 bronze badges 4
  • 2 you have the answer type property can't be changed – mgraph Commented Jun 14, 2012 at 11:19
  • half an answer, still the list :) – ilyes kooli Commented Jun 14, 2012 at 11:20
  • Since you can't change the type of an element, you will have to replace it with a new element. – Darin Dimitrov Commented Jun 14, 2012 at 11:22
  • Good idea to ask about your actual problem from the start. See XY Problem – user1106925 Commented Jun 14, 2012 at 12:04
Add a comment  | 

3 Answers 3

Reset to default 17

You can't change an input's type in IE (<9?) after it has been inserted into the DOM. jQuery raises an error for all browsers because of this.

From source:

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

There are no other cases I know about (or jQuery knows about) and how to get around this depends a lot on what you are trying to do in the first place. Consider creating a new element with different type and using that for example.

Edit: To prevent the button from submitting a form, you can use:

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

Or (credit Tim Down):

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

To prevent form from being submitted through any means, you can use:

$("#form").submit(function(e){
    e.preventDefault();
});

you can replace it:

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

or use event.preventDefault()

Since you just want to disable the submit button:

If you are using jQuery < 1.6 do this:

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

If you are using jQuery 1.6+:

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

See this question: .prop() vs .attr() for references why.

发布评论

评论列表(0)

  1. 暂无评论