doulongsi1831 2011-07-14 20:49
浏览 14
已采纳

表单提交后,jquery删除隐藏的表单值

I have a small form and I need jquery to clear the hidden form value after the form has been submitted.

Here is a snippet from my form

<select id='size'>
    <option value='4.99'>S</option>
    <option value='5.99'>M</option>
    <option value='6.99'>L</option>
    <option value='7.99'>XL</option>
</select>

<input type="hidden" id="my-item-price" name="my-item-price" value="" />

and the jquery which changes the hidden field value

$(document).ready(function(){

    $('#size').change(function() {

        var x= $(this).val();
        $('#my-item-price').val(x);
    });
}

the problem is once i have submitted the form, i want the value to be erased from the hidden input. I expect that i have to set the value to "" but how do i do it?

EDIT

Here is a link to a testing part of my site

http://www.operationbraveheart.org.uk/jcart/

What i want to do is once the button is clicked, it resets the hidden form value to nothing so when i make a new selection, it changes the value again

Here is the code for the jquery.

    function clearoption() {
            alert('it should work');
            $('#my-item-price').attr('value','');
        }

        $('#size').change(function() {

            var x= $(this).val();
            $('#my-item-price').val(x);
        }); 

      // Add an item to the cart
      $('.jcart').submit(function(e) {
         add($(this));
         clearoption();
         e.preventDefault();
      });

It's calling the function to clear the #my-item-price but for some reason it's not doing it. If i make a different size selection it still adds the original price

  • 写回答

1条回答 默认 最新

  • dqbjvg2518 2011-07-17 17:15
    关注

    If you are using Ajax to submit the form then in the success part, you can use following code

     $(' #myform input:hidden').attr('value','');
    

    or

    $('#my-item-price').attr('value','');
    

    else if u r not using ajax for submitting form then clearing hidden fields wont make sense.

    also, it would be a lot easier if u can post link to your page!

    EDIT: To trigger/update the change to the

    tag where the price is displayed, you need to do following:

    $(' #myform input:hidden').attr('value','').trigger('change');
    

    I think this should work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?