locked
Submit form with hidden field value from radio button group RRS feed

  • Question

  • User379720387 posted

    This is my form:

    <form method="get" id="qcktxnform" class="form-horizontal" role="form" action="https://www.sandbox.paypal.com/cgi-bin/webscr">
                            @AntiForgery.GetHtml()
                            
                            <input type="hidden" id="radioSelection" name="a3" value="????" />

    <div class="radio">
    <label>
    <input type="radio" id="rdb1" name="planprice" value="@currentPlans.ElementAt(0).planCurrentPrice">
    @currentPlans.ElementAt(0).planNamePrice
    </label>
    </div>
    <div class="radio">
    <label>
    <input type="radio" id="rdb2" name="planprice" value="@currentPlans.ElementAt(1).planCurrentPrice" checked>
    @currentPlans.ElementAt(1).planNamePrice
    </label>
    </div>
    <div class="radio">
    <label>
    <input type="radio" id="rdb3" name="planprice" value="@currentPlans.ElementAt(2).planCurrentPrice">
    @currentPlans.ElementAt(2).planNamePrice
    </label>
    </div>

    And this is my js:

    <script>
        $(document).ready(function () {
            $('#rdb1').click(function () {
                $('#@currentPlans.ElementAt(0).planCurrentPrice').val(@currentPlans.ElementAt(0).planCurrentPrice);
            });
    
            $('#rdb2').click(function () {
                $('#@currentPlans.ElementAt(1).planCurrentPrice').val(@currentPlans.ElementAt(1).planCurrentPrice);
            });
    
            $('#rdb3').click(function () {
                $('#@currentPlans.ElementAt(2).planCurrentPrice').val(@currentPlans.ElementAt(2).planCurrentPrice);
            });
    
            $(':submit').click(function () {
                alert($('#radioSelection').val());
                document.getElementById('qcktxnform').submit();
                return false;
            });
        });
    </script>

    How do I set the value of hidden field "a3?

    Thursday, September 19, 2019 7:11 PM

Answers

  • User475983607 posted

    Not understanding.
    
    What is theValue?
    

    That's up to you.  Your code is a little confusing.  I think you are after code similar to the following.

    <form method="get" id="qcktxnform" class="form-horizontal" role="form" action="https://www.sandbox.paypal.com/cgi-bin/webscr">
    
        <input type="hidden" id="radioSelection" name="a3" value="????" />
    
        <div class="radio">
            <label>
                <input type="radio" id="rdb1" name="planprice" value="1">
                1
            </label>
        </div>
        <div class="radio">
            <label>
                <input type="radio" id="rdb2" name="planprice" value="2" checked>
                2
            </label>
        </div>
        <div class="radio">
            <label>
                <input type="radio" id="rdb3" name="planprice" value="3">
                3
            </label>
        </div>
    </form>
        <script>
            //Set the hidden field when the page loads
            $('#radioSelection').val($(this).val());
    
            //handler
            $(':radio[name="planprice"]').click(function () {
                //Debug
                console.log($(this).val());
                //Set the input value
                $('#radioSelection').val($(this).val());
    
            });
        </script>

    Keep in mind, the radio button has the state.  It's not clear why you are making a second copy of the radio button list value.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 19, 2019 8:06 PM

All replies

  • User475983607 posted

    Your design is not clear.  Why do you need to set a hidden field when you have the value of the radio button?

    How do I set the value of hidden field "a3?

    $('#radioSelection').val('theValue');

    Thursday, September 19, 2019 7:22 PM
  • User379720387 posted

    Not understanding.

    What is theValue?

    Thursday, September 19, 2019 7:46 PM
  • User475983607 posted

    Not understanding.
    
    What is theValue?
    

    That's up to you.  Your code is a little confusing.  I think you are after code similar to the following.

    <form method="get" id="qcktxnform" class="form-horizontal" role="form" action="https://www.sandbox.paypal.com/cgi-bin/webscr">
    
        <input type="hidden" id="radioSelection" name="a3" value="????" />
    
        <div class="radio">
            <label>
                <input type="radio" id="rdb1" name="planprice" value="1">
                1
            </label>
        </div>
        <div class="radio">
            <label>
                <input type="radio" id="rdb2" name="planprice" value="2" checked>
                2
            </label>
        </div>
        <div class="radio">
            <label>
                <input type="radio" id="rdb3" name="planprice" value="3">
                3
            </label>
        </div>
    </form>
        <script>
            //Set the hidden field when the page loads
            $('#radioSelection').val($(this).val());
    
            //handler
            $(':radio[name="planprice"]').click(function () {
                //Debug
                console.log($(this).val());
                //Set the input value
                $('#radioSelection').val($(this).val());
    
            });
        </script>

    Keep in mind, the radio button has the state.  It's not clear why you are making a second copy of the radio button list value.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 19, 2019 8:06 PM
  • User379720387 posted

    Thanks for pointing out my errors in the script, this is what I ended up with:

    <input type="hidden" id="radioSelection" name="a3" value="" />


    Note that the value attribute is ""
    <script>
        $(document).ready(function () {
            $('#rdb1').click(function () {
                $('#radioSelection').val(@currentPlans.ElementAt(0).planCurrentPrice);
            });
     
            $('#rdb2').click(function () {
                $('#radioSelection').val(@currentPlans.ElementAt(1).planCurrentPrice);
            });
     
            $('#rdb3').click(function () {
                $('#radioSelection').val(@currentPlans.ElementAt(2).planCurrentPrice);
            });
     
            $(':submit').click(function () {
                alert($('#radioSelection').val());
                document.getElementById('qcktxnform').submit();
                return false;
            });
        });
    </script>

    Thursday, September 19, 2019 9:00 PM