简体   繁体   中英

how to replace specfic value from the option tag using jquery

i want to append the text box values in the dropdown list when user can change the text box value that time the existing value should be replaced to changed value in the option tag without selected how i can do this

   var valuedata
   $('.txtbox1').click(function (){
       valuedata = $(this).val();
   });
   $('.txtbox1').focusout(function (){
        var data = $(this).val();
        if ($('.ddl [value ="' + valuedata + '"]').val() == valuedata) {
            alert(valuedata);    
            $('.ddl').append($("<option value=" + data + " ></option>").html(data).val(data));
        }
        else {
            $(".ddl [value='" + valuedata + "']").val(data);
        }
   });

Try this :

$('select option:contains("old_text")').text('new_text');

To check for option is appended or not :

$("select option").each(function(i,obj){
    if($(obj).text() == "your_text"){
        console.log("yes");
    }
})

There are spaces before [value $(".ddl [value='" + valuedata + "']").val(data); which might be problem. So, remove that space:

$(".ddl[value='" + valuedata + "']").val(data);

Use html instead of append like this:

$('.ddl').html($("<option value=" + data + " ></option>")

DEMO: http://jsfiddle.net/c2Ry8/

    $(document).ready(function() {
        $('.txtbox1').focusout(function (){
            var data = $(this).val();
            $('.ddl').append("<option value=" + data + " >"+data+"</option>");
            });
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM