Visualforce 中的命令按钮无法从动态下拉列表中读取所选项目 [英] Command button in Visualforce can't read selected item from dynamic drop down list

查看:30
本文介绍了Visualforce 中的命令按钮无法从动态下拉列表中读取所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求 Visualforce (Salesforce) 页面专家的帮助.

I am looking for help from Visualforce (Salesforce) page guru.

背景:有一个下拉列表(就 Visualforce 页面而言,apex:selectList).它的选择选项是由 jQuery 使用从 RemoteAction(在控制器中)返回的数据(JSON 格式)构建的.

Background: There is a drop down list (apex:selectList in terms of Visualforce page). Its select options are built by jQuery with returned data (in JSON format) from RemoteAction (in Controller).

问题:当我单击将所选项目值发送到控制器的命令按钮时,它总是报告错误消息(j_id0:j_id2:j_id3:j_id4:orgList: Validation Error: Value is not valid).

Problem: it always reports error message (j_id0:j_id2:j_id3:j_id4:orgList: Validation Error: Value is not valid) when I click command button which sends selected item value to Controller.

有什么想法吗?非常感谢.

Any thoughts? Thanks very much.

特洛伊

Visualforce 标记:

Visualforce markup:

<apex:panelGrid columns="2">
<apex:selectList id="orgList" value="{!selected}"  size="1">
</apex:selectList>
<apex:commandButton value="Add" action="{!add}" style="width:80px">
</apex:panelGrid>

JavaScript:

JavaScript:

$j("input[id$='azc']").keyup(function(){
   var op = $j("select[id$=orgList]");
    if($j(this).val().length >= 6){
             op.empty().append('<option value=""></option>');
             OrgController.findOrgs($j(this).val(), function(result, event){
                  if(event.status){
                      var data =  $j.parseJSON('[' + result.replace(/'/g, '"') + ']');
                      $j.each(data, function(){
                          $j('<option />', {value: this['value'], text: this['label']}).appendTo(op);
                      }); 
                  }else{
                      alert(event.message);             
                  } 
              },{escape:true});
        } 

组织控制器

public String selected { get; set; }
public PageReference add(){
    Customer__c customer =  findSelected(selected);
    if(customer != null){
      customer.Pending__c = 'Yes';
      update customer; 
    }
    return null;
}

推荐答案

我会尝试使用普通 (html) 选择列表并将选定的值发送到每个隐藏字段的控制器,如下所示(对我有用):

I would try to use a normal (html) select list and send selected value to controller per hidden field like this (works for me):

<!-- If the user selects an option - we will fill out the hidden field with that value -->
<select size="1" id="orgList" onChange="jQuery('[id$=hiddenField]').val(document.getElementById(this.id).value);">
    <option value="none">--Please select--</option>
</select>

<!-- Now add new options -->
<script>
    var myOptions = {
        val2 : 'Val 2',
        val3 : 'Val 3'
    };

    var mySelect = jQuery('#orgList');

    jQuery.each(myOptions, function(val, text) {
        mySelect.append( jQuery('<option></option>').val(val).html(text) );
    });

</script>

<!-- Here is the hidden field assigned to the apex variable -->
<apex:inputHidden value="{!myselected}" id="hiddenField"/>

<apex:commandButton reRender="none" value="Get value"/>

这篇关于Visualforce 中的命令按钮无法从动态下拉列表中读取所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆