开发者

enable submit button using Jquery if any form element is clicked

I'm using a django template and jquery to create a form.开发者_运维百科 I want the submit button to be disabled unless a user clicks any of the form elements. I tried a simple one-liner that I thought would work, but it doesn't. I also tried attaching the handler to the class "q1_input" and I've tried .focus and .click, but nothing is enabling the button.

Here is the code snippet:

<script type="text/javascript">

    $(document).ready(function() {
        $( "#autofillbox1" ).autocomplete( {"source": {{ choices1|safe }}});
        $( "#autofillbox2" ).autocomplete( {"source": {{ choices2|safe }}});
        $("#brainytop").append($("#heading"));
        $('#q1_form').children().change(function(){$("#q1_submit").removeAttr('disabled');});
    });

</script>
<div id="content">
    {% if show_q1 %}<div class="left">
        <p class="title"><strong>{{ q1.title }}</strong></p>
        <form id="q1_form" action="{{ next }}" method="POST">
        {% csrf_token %}
        {% if q1.answer_type == "longtext" %}
            <textarea class="q1_input" name="r{{ ref1 }}" rows="6" cols="36"></textarea>
        {% else %}{% if q1.answer_type == "slider" %}
            <div id="slider1" style="padding-top:1.5em;padding-right: 1em;padding-left:.5em;overflow: visible;">
            {% for r in q1.responseoption_set.all %}
                <div style="text-align:center; margin-bottom:40px;padding-left:10px;">
                <label>{{ r.text }}</label>
                <select class="q1_input" name="r{{ r.id }}" id="slider{{ r.id }}">
            {% for v in range1 %}
                <option value={{ v }}>{{ v }}</option>
            {% endfor %}
            </select><script>$('#slider{{ r.id }}').selectToUISlider({tooltip: false, labels: {{ labels1 }}}).hide();
                </script></div>{% endfor %}</div>
        {% else %}{% if q1.answer_type == "autofill" %}
            <input name="response" class="q1_input" id="autofillbox1" placeholder="Just start typing..."/>
        {% else %}{% if q1.answer_type == "radio" %}
            {% for r in q1.responseoption_set.all %}
                <span class="r"><input class="q1_input" type="radio" name="r" id="q1_r{{ r.id }}" value="{{ r.id }}"/>
                <label for="q1_r{{ r.id }}">{{ r.text }}</label></span><br>
            {% endfor %}
        {% else %}
            {% for r in q1.responseoption_set.all %}
                <span class="r"><input class="q1_input" type="{{ q1.answer_type }}" name="r{{ r.id }}" id="r{{ r.id }}" value="{{ r.id }}"/>
                <label {% if q1.answer_type == "text" %}class="textanswer"{% endif %}for="r{{ r.id }}">{{ r.text }}</label></span><br>
            {% endfor %}
        {% endif %}{% endif %}{% endif %}{% endif %}
        <input type="hidden" id="qid" name="qid" value="{{ q1.pk }}">
        <input id="q1_submit" class = "submit" type="submit" disabled="disabled" value="Submit"/>
    </form>

what am I doing wrong?

EDIT: upon request, rendered code looks like:

<script type="text/javascript">

    $(document).ready(function() {
        $( "#autofillbox1" ).autocomplete( {"source": []});
        $( "#autofillbox2" ).autocomplete( {"source": []});
        $("#brainytop").append($("#heading"));
        $('#q1_form').children().change(function(){$("#q1_submit").removeAttr('disabled');});
    });

</script>


<div id="content">
    <div class="left">
        <p class="title"><strong>Do you feel comfortable walking alone on campus at night?</strong></p>
        <form id="q1_form" action="/canvas/chart/" method="POST">

        <div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='a1a2fe72c2660b05341051a85351626f' /></div>


                <span class="r"><input class="q1_input" type="radio" name="r" id="q1_r295" value="295"/>
                <label for="q1_r295">Yes</label></span><br>

                <span class="r"><input class="q1_input" type="radio" name="r" id="q1_r296" value="296"/>
                <label for="q1_r296">No</label></span><br>


        <input type="hidden" id="qid" name="qid" value="126">
        <input id="q1_submit" class = "submit" type="submit" disabled="disabled" value="Submit"/>
    </form>

EDIT2:

another interesting thing is that if I click, the disabled attribute is set to "", but there appear to be some custom jquery css settings that are not recognizing the change. Firebug shows:

I'm assuming "aria-disabled" (whatever that is, honestly, I don't know) and the css class are what's preventing the re-enable. Not sure why those aren't getting updated with the attribute removal, though... or how to fix that (easily, at least)


See this jsfiddle I created: http://jsfiddle.net/6DXSa/

Functionality works perfectly, and it's basically the code you already have. There must be something else not included here that's conflicting.

UPDATE This jsfiddle actually uses your HTML source and still works fine: http://jsfiddle.net/6DXSa/1/


As of jQuery 1.6 you cannot use .removeAttr() to successfully change certain attributes. Its a bit confusing, but basically jQuery is separating out actual html attributes (name, id, etc.) and js object properties that can be represented as attributes (checked, disabled). Use .removeProp() instead of .removeAttr().

See the jQuery docs on .removeProp() for more info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜