Adding value to input field with jQuery

后端 未结 4 2225
长发绾君心
长发绾君心 2021-02-09 04:25

I want to add some value in an input field with jQuery. The problem is with the ID of the input field. I am using the id such as options[input2]. In that case my co

相关标签:
4条回答
  • 2021-02-09 04:48
    $.each(obj, function(index, value) {
        $('#looking_for_job_titles').tagsinput('add', value);
        console.log(value);
    });
    
    0 讨论(0)
  • 2021-02-09 04:50

    You can simply use

     $(this).prev().val("hello world");
    

    JSFiddle Demo

    0 讨论(0)
  • You can do it as below.

    $(this).prev('input').val("hello world");
    

    Live Demo

    0 讨论(0)
  • 2021-02-09 05:02

    You have to escape [ and ]. Try this:

    $('.button').click(function(){
        var fieldID = $(this).prev().attr("id");
        fieldID = fieldID.replace(/([\[\]]+)/g, "\\$1");
        $('#' + fieldID).val("hello world");
    });
    

    Demo: http://jsfiddle.net/7RJtf/1/

    0 讨论(0)
提交回复
热议问题