2010-11-23 54 views
4

我不是很擅長Jquery。我想ID添加到一個隱藏字段,當用戶選擇自動完成項目JQuery自動完成存儲ID在隱藏輸入

HTML

<input type="hidden" value="" name="CountryId" id="CountryId"> 

的Javascript

$(document).ready(function() { 
    $("#Country").autocomplete({ 
     source: function (request, response) { 
      $.ajax({ 
       url: "/Address.mvc/CountryList", type: "POST", dataType: "json", 
       data: { searchText: request.term, maxResults: 10 }, 
       success: function (data) { 
       response($.map(data, function (item) { 
        return { label: item.Value, value: item.Value, id: item.Key } 
       })) 
      } 
     }) 
    }, 
    select: function (event, ui) { 
     alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.id) : "Nothing selected, input was " + this.value); 
     $('#CountryId').val = ui.item.id; 
     console.log($('#CountryId').val + ' has faded!'); 
    }, 
    change: function (event, ui) { 
     alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.id) : "Nothing selected, input was " + this.value); 
     $('#CountryId').val = ui.item.id; 
     console.log($('#CountryId').val+'has faded!') 
    } 
}); 

控制檯顯示的值是

function (value) 
    { if (!arguments.length) 
     { var elem = this[0]; 
     if (elem) 
      { if (jQuery.nodeName(elem, "option")) 
       { var val = elem.attributes.value; return !val || val.specified ? elem.value : elem.text;} 
      if (jQuery.nodeName(elem, "select")) 
       { var index = elem.selectedIndex, values = [], options = elem.options, one = elem.type === "select-one"; 
       if (index < 0) { return null; } 
       for (var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++) 
        { var option = options[i]; 
        if (option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) && (!option.parentNode.disabled || !jQuery.nodeName(option.parentNode, "optgroup"))) 
         { value = jQuery(option).val(); 
         if (one) { return value; } 
         values.push(value); 
         } 
        } return values; 
       } 
      if (rradiocheck.test(elem.type) && !jQuery.support.checkOn) 
       { return elem.getAttribute("value") === null ? "on" : elem.value; 
       } 
      return (elem.value || "").replace(rreturn, ""); 
      } 
     return undefined; 
     } 
    var isFunction = jQuery.isFunction(value); 
    return this.each(function (i) 
     {var self = jQuery(this), val = value; 
     if (this.nodeType !== 1) {return;} 
     if (isFunction) {val = value.call(this, i, self.val());} 
     if (val == null) {val = "";} else if (typeof val === "number") {val += "";} 
     else if (jQuery.isArray(val)) {val = jQuery.map(val, function (value) {return value == null ? "" : value + "";});} 
     if (jQuery.isArray(val) && rradiocheck.test(this.type)) {this.checked = jQuery.inArray(self.val(), val) >= 0;} 
     else if (jQuery.nodeName(this, "select")) {var values = jQuery.makeArray(val); 
     jQuery("option", this).each(function() {this.selected = jQuery.inArray(jQuery(this).val(), values) >= 0;}); 
     if (!values.length) {this.selectedIndex = -1;}} else {this.value = val;} 
     }); 
    } 
has faded! 

的val似乎將JQuery核心腳本中的函數val帶回,而不是隱藏字段的值。

我在做什麼錯?

回答

5

設置的值是通過將值在括號中,而不是做等號:http://www.w3schools.com/jquery/html_val.asp

所以不是

$('#CountryId').val = ui.item.id; 

嘗試

$('#CountryId').val(ui.item.id); 
+0

你是右我已經在網上搜索至少一個小時,並沒有絲毫的暗示,我把它分配錯了。非常感謝。 – TheMar 2010-11-23 18:03:47