在jQuery中启用或禁用表单元素 [英] Enable or disable form elements in jquery

查看:46
本文介绍了在jQuery中启用或禁用表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jquery正确禁用和启用表单元素.单击选择时,我需要禁用文本表单元素.反之亦然.

How do I properly disable and enable form elements using jquery. I need to disable the text form element when I click on the select. And vise versa.

<html>
    <head>
    <script src="jq.js"></script>
    <script>

    $(function(){

    $('#fromdate').click(function(){
        $('#yosh').attr('disabled','disabled');
        $('#fromdate').removeAttr('disabled');
    });

    $('#yosh').click(function(){
        $('#yosh').removeAttr('disabled');
        $('#fromdate').attr('disabled','disabled');
    });

    });
    </script>

    </head>
    <body>
    Sort by:
    <select name="yosh" id="yosh">
    <option value="daily">daily</option>
    <option value="weekly">yesterday</option>
    <option value="weekly">weekly</option>
    <option value="monthly">monthly</option>
    <option value="yearly">yearly</option>

    </select><br/>
    date range:<br/>

    From:<input type="text" value="" name="fromdate" id="fromdate"></input><br/>
    To:<input type="text" value="" name="todate" id="todate"></input><br/>


    Customer:<input type="text" value="" name="customer" id="customer"></input>

    </body>

    </html>

推荐答案

我在评论中写过的示例:

Example of what i've written in the comment:

<span id="spnSel">
    <select name="yosh" id="yosh">
        <option value="daily">daily</option>
        <option value="weekly">yesterday</option>
        <option value="weekly">weekly</option>
        <option value="monthly">monthly</option>
        <option value="yearly">yearly</option>
    </select>
</span>

添加此事件:

$('#spnSel').mouseover(function () {
   $('#yosh').prop('disabled', false);
});

您也可以在文本框中执行此操作.这可能不是最好的解决方案/方法,但可以完成工作.

You can do this for the text boxes as well. This may not be the best solution/approach but it will do the job.

这篇关于在jQuery中启用或禁用表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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