开发者

Adding two javascript functions

开发者 https://www.devze.com 2023-03-01 12:51 出处:网络
Hey guys, i have a javascript function which tallys up the rel value of checkboxes and displays the answer, I now have a <select> statement with 3 values. Each value has a different fiscal value

Hey guys, i have a javascript function which tallys up the rel value of checkboxes and displays the answer, I now have a <select> statement with 3 values. Each value has a different fiscal value i.e 40,50 & 60. I cant figure out a java statement that will add the fiscal value of my select statement selection(tong twister) and add it to my existing java function below.

  $(document).ready(function() {
        function recalculate() {
            var sum = 0;

            $("input[type=checkbox]:checked").each(function() {
                 sum += parseInt($(this).attr("rel"));
            });

            $("#output").html(sum);
        }

        $("input[type=checkbox]开发者_运维知识库").change(function() {
            recalculate();
        });

    });




   <td width="236" height="25" align="left">Booking Duration: </td>
    <td width="548" height="23"><select name="duration" id="duration1">
    <option value="One Day" id="one" rel="40">One Day</option>
    <option value="Two Day" id="two" rel="50">Two Day</option>
    <option value="Weekend" id="weekend" rel="60">Weekend</option>
    </select>

The output of my initial function is displayed here <span id="output"></span>

How do I go about writing a second java that adds the rel value of my select statement with the sum value of my initial javascript function.

Sorry I dont think i made my question clear. I already have a series of checkboxes which the above code adds up the rel values of the checked check boxes and displays. I now ALSO have a select statement with three options each which have a relf value. I now want to write some code that adds the rel values of the checkboxes AND that of the select statement.


You could use the 'value' property of the option elements to contain the number rather than the 'rel'. Jquery (which it looks like you're using) can fetch that property using the .val() function, or you can use vanilla Javascript to do it. Once you've fetched the value, you can do what you like with it.

You could store the result of the original function in a global variable that the second function can access - that's one way of doing it.


I think your problem resides in your HTML, you are talking about "checkboxes" and you consider so in your javascript, BUT your HTML displays a SELECT ..

What about this for your HTML

<td width="236" height="25" align="left">Booking Duration: </td>
<td width="548" height="23">
<input type="checkbox" name="duration" id ="one" value="One" /> One
<input type="checkbox" name="duration" id ="two" value="Two" /> Two
<input type="checkbox" name="duration" id ="weekend" value="Weekend" /> Weekend
0

精彩评论

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

关注公众号