首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jquery中的onchange问题

jquery中的onchange问题
EN

Stack Overflow用户
提问于 2014-12-19 14:21:05
回答 1查看 56关注 0票数 0

我正在创建选择框,并且我在jquery.it中为选择框编写了更改函数,运行良好。但我的问题是,当我将光标移动到选择框选项中,而没有选择任何选项时,我会在选择框外单击。但是change函数正在调用,我不知道如何修复这个问题,我的代码如下

我的小提琴是http://jsfiddle.net/6mhed8yL/

代码语言:javascript
运行
复制
<select name="selecting" id="selecting" >
    <option selected="selected" >--Control--</option>
    <option id="Select All ">Select All</option>
    <option id="Remove All " >Remove All</option>
    <option id="Sample Questions" >Sample Questions</option>
    <option id=" User Added Questions">User Added Questions</option>
    <option id="Show All Questions" >Show All Questions</option>
                </select>

Jquery

代码语言:javascript
运行
复制
$('#selecting').change(function(){
        alert("check");
        var opt=$('#selecting').val();
        var url="";
        alert("option is"+opt);
        //  $("#selecting option:selected").removeAttr('disabled');
             //$( "#selecting option:disabled" ).val( opt );
            //$("#selecting option[value=" + opt + "]").attr('disabled', 'disabled');


        if(opt=="Select All")
            {
             $('input:checkbox').prop('checked', true);
             $("select option:contains('Select All')").attr("disabled","disabled");
             $("select option:contains('Remove All')").removeAttr("disabled");
             $("select option:contains('Sample Questions')").removeAttr("disabled");
             $("select option:contains('User Added Questions')").removeAttr("disabled");
             $("select option:contains('Show All Questions')").removeAttr("disabled");


            }
        else if(opt=="Remove All")
            {
            $("input:checkbox").prop("checked", false); 
             $("select option:contains('Remove All')").attr("disabled","disabled");
             $("select option:contains('Select All')").removeAttr("disabled");
             $("select option:contains('Sample Questions')").removeAttr("disabled");
             $("select option:contains('User Added Questions')").removeAttr("disabled");
             $("select option:contains('Show All Questions')").removeAttr("disabled");
            }
        else if(opt=="Sample Questions")
            {

             $("select option:contains('Sample Questions')").attr("disabled","disabled");
             $("select option:contains('Select All')").removeAttr("disabled");
             $("select option:contains('Remove All')").removeAttr("disabled");
             $("select option:contains('User Added Questions')").removeAttr("disabled");
             $("select option:contains('Show All Questions')").removeAttr("disabled");
            url = "SampleQuestions.jsp";
            alert("sample");

            /*  if (window.XMLHttpRequest)
                    req = new XMLHttpRequest();
                    else if (window.ActiveXObject)
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                    req.open("GET", url, true);

                    req.onreadystatechange =callback;

                    req.send(null); */



            }
        else if(opt=="User Added Questions")
        {
             $("select option:contains('User Added Questions')").attr("disabled","disabled");
             $("select option:contains('Select All')").removeAttr("disabled");
             $("select option:contains('Remove All')").removeAttr("disabled");
             $("select option:contains('Sample Questions')").removeAttr("disabled");
             $("select option:contains('Show All Questions')").removeAttr("disabled");
             $("select option:contains('Show All Questions')").removeAttr("disabled");

            url="UserAddedQuestion.jsp";
            alert("user");

        /*   if (window.XMLHttpRequest)
                    req = new XMLHttpRequest();
                    else if (window.ActiveXObject)
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                    req.open("GET", url, true);

                    req.onreadystatechange =callback;

                    req.send(null);
                     */

        }
        else if(opt=="Show All Questions")
            {

             $("select option:contains('Show All Questions')").attr("disabled","disabled");
             $("select option:contains('Select All')").removeAttr("disabled");
             $("select option:contains('Remove All')").removeAttr("disabled");
             $("select option:contains('Sample Questions')").removeAttr("disabled");
             $("select option:contains('User Added Questions')").removeAttr("disabled");
            url="ShowAllQuestions.jsp";
            alert("all");

            /*  if (window.XMLHttpRequest)
                    req = new XMLHttpRequest();
                    else if (window.ActiveXObject)
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                    req.open("GET", url, true);

                    req.onreadystatechange =callback;

                    req.send(null);
                     */

            }



    });
EN

回答 1

Stack Overflow用户

发布于 2014-12-19 14:25:45

我已经更新了jsfiddle,现在正在工作。

请在此处查看[fiddle][1] http://jsfiddle.net/6mhed8yL/1/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27560722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档