jQuery removeAttr('type')不起作用 [英] jQuery removeAttr('type') doesn't work

查看:706
本文介绍了jQuery removeAttr('type')不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单

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

触发萤火虫错误

  type属性不能更改

我有2个问题: / p>


  • 如何解决此问题?

  • 是否有一个可以' t改变?


    感谢

    编辑 >



    我想要做的是:

    防止按钮提交表单。



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

    类似于:


      include'form.php'; 
    包含'buttons.php';

    其中我只能控制 buttons.php

    解决方案

    您无法更改输入的在IE(<9?)中被插入到DOM之后。 jQuery为所有浏览器产生错误,因为这一点。



    来源:

      //我们不能允许改变type属性(因为它在IE中导致问题)
    if(rtype.test(elem.nodeName )&& elem.parentNode){
    jQuery.error(type属性无法更改);

    我没有其他的情况以及如何解决这个问题很大程度上取决于你想要做什么。请考虑创建一个不同类型的新元素并使用它。



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

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

    或(信用Tim Down):

      $(#button)。prop(disabled,true); 






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

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


    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

    解决方案

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

    这篇关于jQuery removeAttr('type')不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆