启用和禁用仅处理文本输入类型的功能,建议?

时间:2013-01-14 21:49:56

标签: jquery html input

我尝试更改输入标记的输入类型,但启用和禁用功能似乎不能在整数上工作,只能在文本字段上工作。我该如何解决这个问题?

我的提交是明天,这是我的搜索代码:

 <script type="text/javascript">

            $('#exactButton').live('click', function(){
                $(this).prev().prev().prev().prev().prev().removeAttr('disabled');

                $(this).prev().prev().prev().attr('disabled',true);
                $(this).prev().prev().prev().prev().attr('disabled',true); 
            });

            $('#rangeButton').live('click',function(){
                $(this).prev().prev().removeAttr('disabled');
                $(this).prev().prev().prev().removeAttr('disabled');

                $(this).prev().prev().prev().prev().attr('disabled',true);
            });

        })
    </script>

这是我的HTML代码:

 <button id="button">Add List</button><br><br>
        <form id ="form" name="search" method="get" action="test.php">
            <div id="div">
                <select name ="select" >
                    ...options...
                </select>

                Value:<input type="text" name="exact" id="exactField" />

                From: <input type="text" name="from" id="fromField" />
                To: <input type="text" name="to" id="toField" />

                <br>
                <input type="button" name="answer" value="Range" id="rangeButton" />
                <input type="button" name="answer" value="Exact" id="exactButton" />

            </div>
            <br>
            <input type="submit"name="search" value="Submit">
        </form>

提前谢谢..

3 个答案:

答案 0 :(得分:2)

没有输入类型“整数”。只有textpasswordcheckboxradiosubmitresetfilehiddenimagebuttonHTML 4。大多数网站使用“文本”作为数字;这种缺乏特异性是JavaScript的最初原因之一:检测人们将字母输入到用于数字的文本字段中(和/或警告他们在提交之前需要修复这些值)。

HTML 5中,添加了其他输入类型:searchtelurlemaildatetime,{{1} },datemonthweektimedatetime-localrange现已推出;这使得标记更具可读性,并允许开发人员将一些客户端验证留给浏览器。

目前推荐的数字输入方法是使用color。较旧的非HTML 5浏览器将未知类型呈现为type="number",因此当支持不可用时不会丢失任何内容。

有关启用/禁用字段的正确方法,请参阅jQuery - Disable Form Fields。输入字段的类型是无关紧要的 - jquery足够聪明,可以为您做正确的事。

答案 1 :(得分:1)

首先。不要使用jquery的“live”api它将被弃用。 http://api.jquery.com/live/

注意:对于“on”api,你需要最新的jquery。

你是否想要这样做

更新了HTML

        <button id="button">Add List</button><br><br>
          <form id ="form" name="search" method="get" action="test.php">
           <div id="maindiv">
             <div id="div">
            <select name ="select" >
                ...options...
            </select>

            Value:<input type="text" name="exact" id="exactField" />

            From: <input type="text" name="from" id="fromField" />
            To: <input type="text" name="to" id="toField" />

              </div>
            </div>
            <br>
            <input type="button" name="answer" value="Range" id="rangeButton" />
            <input type="button" name="answer" value="Exact" id="exactButton" />
        <br>
        <input type="submit"name="search" value="Submit">
    </form> 

更新了javascript

           // When exact button is clicked, disable from and exact field and enable exactfield text field
    $(document).live('click','#exactButton', function(){
        $('#exactField').removeAttr('disabled');
        $('#fromField,#toField').attr('disabled',true);
    });

    // When range button is clicked, enable from and exact field and disable exactfield option
    $(document).on('click','#rangeButton',function(){
        $('#fromField,#toField').removeAttr('disabled');
        $('#exactField"]').attr('disabled',true);
    });

    $('#button').click(function(){
       $('#maindiv').append($('#div').clone());
     });

此代码适用于我的数字和字符。如果我错过任何东西,请告诉我。

答案 2 :(得分:1)

您使用的是什么版本的jquery?

无论如何,在1.6中,要更改disabled属性,您应该使用.prop()函数。

例如$(this).prop('disabled', true);

相关问题