2011-10-20 67 views
0

我有這一般發生在表單中的所有選擇框選項jsonfying選擇框中選擇

<option val='1'> text1 </option> 
<option val='2'> text2 </option> 
<option val='3'> text3 </option> 
<option val='4'> text4 </option> 
<option val='5'> text5 </option> 

現在我想所有這一切都轉換爲jqGrid的理解其下拉菜單中的格式的服務器端查詢。即{1:text1,2:text2 ....}條件是我不會碰到服務器端代碼。我需要通過添加一個通用函數在客戶端修改它。現在,我通過一個jQuery的AJAX調用

getGridDropDown: function (url) { 
     $.ajax({ 
     type: "GET", 
     url: url, 
     dataType: "html", 
     success: function (html) { 
     $(html).find('option').each(function(key){ 
      alert(key) 
     }) 


     }, 
     error: function() { 
     console.log("Error in ajax call to url: " + url); 
     } 
    }); 
}, 

現在,我嘗試過各種形式得到這一點,唯一的辦法看起來像使用正則表達式是唯一的出路。我不能作爲一個jQuery變量處理HTML返回變量,在那裏我可以說$ this.val()+ this.text()

+0

請問您的AJAX調用返回只有'

+0

只有選項..! –

+0

@SaiKrishna好吧,如果任何答案有效,請打勾:) – Val

回答

1

如果您的服務器可以在<select>中生成<options>的列表,則可以在editoptionssearchoptions中直接使用dataUrl

如果您的服務器只能生成的<options>列表而不<select></select>,你不能改變在服務器端的行爲,你可以使用buildSelect來解決這個問題:

editoptions: { 
    dataUrl: 'yourUrl', 
    buildSelect: function (data) { 
     return "<select>" + data + "</select>"; 
    } 
} 

(以老版本的jqGrid它的使用情況下,可能需要測試typeof(data)和使用datadata.responseText

ajaxSelectOptions: { cache: false }的使用可能也是必需的(見here

+0

謝謝你的回答。 –

+1

@Sai Krishna:不客氣!你試過這個嗎?它在你的環境中工作嗎? – Oleg

+0

太棒了,我試過了。它比我的修復更好。我的修補程序添加了undefined作爲其中一個參數 –

1

要使用查找返回的html需要一個有效的根元素。

嘗試以下操作:

$("<div/>").append(data).find('option') 

Example on jsfiddle

+0

謝謝你的回答 –

1
var obj; 
$('option').each(function (i,n){ 
    obj[i] = $(n).text(); 
}); 

的console.log(OBJ); //輸出你的信息...您的控制檯上

我覺得這,應該這樣做,但修復選擇器,讓你有#select_tag_id option

+0

謝謝你的回答 –

0

它很安靜容易。一個簡單的技巧做

getGridDropDown: function (url) { 
     $.ajax({ 
      type: "GET", 
      async:false, 
      url: url, 
      dataType: "json", 
      success: function (html) { 
      data = new String(); 
      $(html).each(function(key){ 

/* jqGrid的選擇選項格式*/

 data += this.value +":"+this.label+";"; 
      }) 
      gridParams['data'] = data  
      }, 
      error: function() { 
      console.log("Error in ajax call to url: " + url); 
      } 
     }); 
    }, 

謝謝你的答案