简体   繁体   English

如何使用jQuery替换选项标签中的特定值

[英]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); [value $(".ddl [value='" + valuedata + "']").val(data);前有空格$(".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: 使用html而不是像这样追加:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从选项标签值属性替换值 - How can i replace value from the option tag value attribute 如何使用jQuery数据表的onChange事件从html select标签选项值中获取值? - How to get value from html select tag option value using onChange event using jQuery data table? 如何使用 JavaScript\\JQuery 使用选择标记的选定选项的值设置隐藏输入标记的值? - How can I set the value of an hidden input tag with the value of the selected option of a select tag using JavaScript\JQuery? 如何<a>使用jQuery</a>替换<a>部分中的</a>最后一个<a>标签</a> - How to replace last <a> tag from section using jquery 如何使用选项标签的文本 jQuery 中的 select 选项 - How to select option in jQuery using text of option tag 如何使用javascript和jquery从html元素到特定的html元素字段分别获取值 - How to get values separately from html elements to specfic html element field using javascript and jquery 如何使用jQuery替换图像src标签 - How to replace the image src tag using jquery 使用 jquery 在多个选择标签中获取选项值 - Get option value in multiple select tag using jquery 选择选项标签中的值时如何从json数组中获取更多数据 - How to get more data from json array when a value in option tag is selected jquery 如何使用jQuery从“ div”标签的CSS中获取值? - How to get the value from css of “div” tag using jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM