问题
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.
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.
my controller:
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;
}}
回答1:
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
来源:https://stackoverflow.com/questions/48729958/visualforce-javascript-remoting-doesnt-work-properly