javascript远程处理无法正常工作

javascript远程处理无法正常工作

本文介绍了visualforce javascript远程处理无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一些html元素作为对我的visualforce页面中的按钮单击的响应,并且我正在使用javascript远程处理,但是无论我做什么,在单击按钮后页面始终保持刷新状态.

I'm trying to create some html elements as a reponse to a button click in my visualforce page, and i am using javascript remoting, but no matter what i do the page keeps refreshing after the button click.

我的visualforce页面:

my visualforce page:

<apex:page Controller="BpmIcountPayment">
<head>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css"/>
</head>
<body>
<script>

    function addProductRow(e) {
        e.preventDefault();
        var productId = $('select[id$=productsLov]').val();
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.BpmIcountPayment.getProductRowData}',
            productId,
        function(result, event) {
            if  (event.status) {
                productRowHtml = '<div id="p-' + result.Id + '">';
                productRowHtml += '<span>' + result.Description + '<span>';
                productRowHtml += '<button class="plusButton">+</button><input type="number">1</input><button class="minusButton">-</button>';
                if (result.Name == 'discount') {
                    productRowHtml += '<input classtype="number"></input><span>₪</span>';
                };
                productRowHtml += '<span>' + result.Price + '₪</span>';
                $('div[id$=productRows]').append(productRowHtml);
            } else if (event.type === 'exception') {
                console.log(event.message + '   ' + event.where);
            } else {
                console.log('else   ' + event.message);
            }
        }, {escape: true});
    }

</script>
</body>
<apex:form >
<div>
    <apex:selectList id="productsLov" value="{!productsTitle}" multiselect="false" size="1">
        <apex:selectOptions value="{!ProductsLov}"></apex:selectOptions>
    </apex:selectList>
    <button id="addProductButton" onclick="addProductRow()" reRender="false">add product</button>
</div>
<div id="productsRows">
</div>
</apex:form>
</apex:page>

我什至设法将结果打印到控制台中,但是刷新页面后才这样做.

I even managed to print the result into the console, but it does so after refreshing the page.

我的控制器:

public class BpmIcountPayment{

private final Account account;

public String productsTitle {
  get { return 'products'; }
  set;
}

public List<Product2> productsList {
  get { return productsList; }
  set { productsList = value; }
}

public BpmIcountPayment() {
    account = [SELECT Id, Name, Site FROM Account
               WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
}

public Account getAccount() {
    return account;
}

@RemoteAction
public static Product2 getProductRowData(string productId) {
    Product2 product = [SELECT Id, Name, Family, Price__c, Description
                        FROM Product2
                        WHERE Id = :productId];

    return product;
}

public List<SelectOption> getProductsLov() {
    List<SelectOption> products = new List<SelectOption>();
    productsList = [SELECT Id, Name, Family, Price__c, Description
                    FROM Product2
                    WHERE (Family = 'ShopProduct')
                    OR (Family = 'CourseParent')
                    OR (Family = 'SFCourseProgram')];

    for (Product2 currProduct : productsList) {
        products.add(new SelectOption(currProduct.Id, currProduct.Name));
    }

    return products;
}}

推荐答案

我发现了问题!将按钮类型定义为按钮"只是一种情况,因为默认值为提交",这会导致页面重新呈现

I found the issue! It was just a case of defining the button type as "button" because the default was "submit" and that caused the page to re-render

这篇关于visualforce javascript远程处理无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:17