开发者

Simple calculator using Drupal?

开发者 https://www.devze.com 2023-01-10 02:59 出处:网络
I need to create a simple form which asks two values from the user, and when she submits the form shows a value calculated based on those values.

I need to create a simple form which asks two values from the user, and when she submits the form shows a value calculated based on those values.

For example, user could be shown something like this:

1st number:
2nd number:
Result:
Calculate!

The only editable fields are 1st and 2nd number. After giving the numbers and clicking Calculate!, she sees the following page:

1st number: 15
2nd number: 20
Result: 35
Calculate!

She can now modify the numbers and reclick Calculate! It is OK to submit and reload the page, the values don'开发者_运维问答t need to be updated in-place.

Do I have to do this using JavaScript injection, or is there another way?


Take a look at Computed Field module.


You don't have to do something like this with JavaScript, but that would be the quickest and easiest solution. Especially since you have jQuery available. You don't even have to let users click calculate.

example code:

$("#input_1, #input_2").change(function(){
  $("#result").text(parseInt($("#input_1").val(), 10) + parseInt($("#input_2").val(), 10));
});

given two input fields with id input_1 and input_2 and a span/div etc. to display the result with id result the above code automatically calculate the two values whenever something is new is entered into them.


I see here several possible options:

1) You can even create a simple form and, using JavaScript, calculate the data entered in the fields, you can even use simple jQuery, as already suggested in one of the answers.

2) Search for ready-made modules on the official Drupal website, for example:

  • WebForm Calculator
  • Simple Calculator

True, everything will probably have to be refined, but I think it will not take many lines of code.

3) Use the builder uCalc - it does not require programming and all actions occur in the visual editor. You just throw the necessary widgets into the form and do the calculation for the values ​​you need. Very useful thing and saves time.

4) There are a few more websites that can be downloaded ready-made scripts of such calculators, but they will need to be seriously developed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号