开发者

jQuery select list item entering problem

开发者 https://www.devze.com 2022-12-30 10:26 出处:网络
$().ready(function () { $(\'#remove\').click(function () { return !$(\'#FeatureLists option:selected\').remove();
$().ready(function () {

    $('#remove').click(function () {

        return !$('#FeatureLists option:selected').remove();

    });

    $("#add").click(function () {

        var vals = $("#txtaddfeature").val();

        if (vals != '')


            $("#FeatureLists").prepend("<option value=" + vals + " selected='selected'>" + vals + "</option>");

        $("#txtaddfeature").val() = ""

    });
});

The thing is that if I enter Add it will开发者_C百科 go into the option without any problem, but if I enter Manage People only Manage will go as there is a space gap between Manage and People . How will I solve this bug?


Following up from what Simon Thompson said, you need to put the attributes in quotes. Also, it's good practice to use single quotes in javascript, so that you can use double quotes in attribute values. Also, you can't set the result of a function call ($("#txtaddfeature").val() = "").

$().ready(function () {
    $('#remove').click(function () {
        return !$('#FeatureLists option:selected').remove();
    });

    $('#add').click(function () {
        var vals = $('#txtaddfeature').val();

        if (vals != '')
            $('#FeatureLists').prepend('<option value="' + vals + '" selected="selected">' + vals + '</option>');

        $('#txtaddfeature').val('');
    });
});


option value=managed people

will only take managed as there is a space, need to have

option value='manged people'

jquery would be

$("#FeatureLists").prepend("" + vals + "");

0

精彩评论

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