开发者

jquery enable and disable

I added a disable/enable feature but the select menus are always disabled. I would like to enable them based on whether or not a user clicks a check box. User reply check box enables #usernames and text effects check box enables #boxcode, #colors.

Also I have a clear function I want to use to remove the values from #display, #message, #names, #boxcode but it doesn't do anything when the button is click开发者_运维知识库ed.

Jquery

function userreply() {
    $('#display').text('startreply ' + 'middlereply ' + $('#usernames').val() + ' endreply ' + $('#message').val());
    $("input:checkbox").click(function() {
        $("#usernames").attr("disabled", !this.checked);
    });
}

function texteffect() {
    $('#display').text('starttag '
    $('#message').val() + ' endtag ' + $('#boxcode').val());
    $("input:checkbox").click(function() {
        $("#boxcode, #colors").attr("disabled", !this.checked);
    });
}

function clear() {
    $('#display, #message, #names, #boxcode').reset();
}

$('#message').keyup(userreply);
$('#usernames').change(userreply);
$('#boxcode').change(texteffect);

HTML

<form>
    <input type="checkbox" name="enableuserreply" onclick="enable_text(this.checked)" /> Enable User Reply<br />
    <input type="checkbox" name="enapletexteffect" onclick="enable_text(this.checked)" /> Enable Text Effect<br />
    <select id="usernames" disabled="disabled">
        <option value="">Users</option>
        <option value="Alex">Alex</option>
        <option value="Jeff">Jeff</option>
        <option value="Amy">Amy</option>
        <option value="Kate">Kate</option>
    </select>
    <select id="boxcode" disabled="disabled">
        <option value="">Codes</option>
        <option value="[b][/b]">Bold</option>
        <option value="[i][/i]">Italics</option>
        <option value="[u][/u]">underline </option>
        <option value="[s][/s]">Strike</option>
        <option value="[sup][/sup]">Super Script</option>
        <option value="[sub][/sub]">Sub Script</option>
        <option value="[center][/center]">Center</option>
        <option value="[big][/big]">Big</option>
        <option value="[small][/small]">Small</option>
        <option value="[br]">Line Break</option>
        <option value="[url=][/url]">Link</option>
        <option value="startpicture endpicture">Picture</option>
        <option value="[color=][/color]">Color</option>
    </select>
    <select id="colors" disabled="disabled">
        <option value="">Color</option>
        <option value="red">red</option>
        <option value="blue">blue</option>
        <option value="green">green</option>
        <option value="white">white</option>
    </select>
    <input id="message" type="text" />
    <input type="button" value="Clear" onclick="clear()" />
</form>

<pre id="display"></pre>


As for your enable disable problem have a look at this JsFiddle.

I changed checkboxes names to ids and completely rewrote the click handlers to work with jQuery 1.6.

BTW there were quite many syntax errors in your code :)

Have fun

K


change has problems in IE try using live like $('#usernames').live("change", userreply);

I doubt if you can reset the SELECT using reset. You can reset the form which will reset all elements in it. $('#formId')[0].reset();// OR just this.form.reset()

If you want to save some elements on the form then just store their value, reset form, set the values stored back.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜