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

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

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)