如何通过javascript在html中追加值

时间:2015-07-06 12:43:38

标签: javascript jquery html autocomplete

我需要什么

  • 我需要在html属性值中附加数据。

js code

var industrys=[];

for (var b = 0; b < obj.industry.length; b++)
{

    var indus=obj.industry[b];
    var temp = new Object();
    temp["value"] =indus.industry_url;
    temp["data"] = indus.id;
    industrys.push(temp);

}
$('.autocomplete_ind').autocomplete({
    lookup: industrys,

    onSelect: function(suggestion) {
        make_url('', suggestion.value);
        var thehtml = '<strong>industry name:</strong> ' + suggestion.name + ' selected<br> <strong>'+ suggestion.id+'</strong>';
        $('#outputcontent').html(thehtml);
        $(this).attr("value", suggestion.value);
    }

html

<input type="text"  autocomplete="off"  onChange="make_url()"
     id="industry_name"  class="biginput autocomplete_ind" 
     value="all data">

问题

  • 关于执行查看源值=“所有数据”
  • 我做错了。
  • 我试过$(this).attr(“value”,suggestion.value);但是在变革事件中,它反映了价值 - “blala等”。

  • 但刷新或重定向值未附加

像这样

<input type="text" autocomplete="off" onchange="make_url()" id="industry_name" class="biginput autocomplete_ind" placeholder="All Industry" value="All industry">

现在$(this).attr(“value”,suggestion.value);执行

<input type="text" autocomplete="off" onchange="make_url()" id="industry_name" class="biginput autocomplete_ind" placeholder="All Industry" value="agriculture-forestry">

但是在查看源值的页面刷新或重定向时不存在。

1 个答案:

答案 0 :(得分:2)

data()用于设置data-*属性的值。

使用val()设置元素值。

更改

$(this).data('value', suggestion.value);

$(this).val(suggestion.value);

修改

使用append()

附加数据

更改

$('#outputcontent').html(thehtml);

$('#outputcontent').append(thehtml);