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

图片转代码服务由CSDN问答提供 功能建议

我有一个小表单,我需要jquery在表单提交后清除隐藏的表单值。

以下是我的表单中的代码段 <前> &lt; select id ='size'&gt; &lt; option value ='4.99' &gt; S&lt; / option&gt; &lt; option value ='5.99'&gt; M&lt; / option&gt; &lt; option value ='6.99'&gt; L&lt; / option&gt; &lt; option value ='7.99 '&gt; XL&lt; / option&gt; &lt; / select&gt; &lt; input type =“hidden”id =“my-item-price”name =“my-item-price”value =“”/&gt;

和更改隐藏字段值的jquery

  $(document).ready(function()  {
 
 $('#size')。change(function(){
 
 var x = $(this).val(); 
 $('#my-item-price')。val  (x); 
}); 
} 
   
 
 

问题是我提交表单后,我希望从隐藏的输入中删除值 我希望我必须将值设置为“”但是我该怎么做?

编辑

这是一个链接 到我网站的测试部分

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

我想要做的是点击按钮后,它会将隐藏的表单值重置为空,这样当我进行新的选择时 ,它再次更改值

以下是jquery的代码。

  function clearoption(){
 alert('it should work'); 
 $('#my-item-price')。attr('value',  ''); 
} 
 
 $('#size')。change(function(){
 
 var x = $(this).val(); 
 $('#my-item  -price')。val(x); 
});  
 
 //将项目添加到购物车
 $('。jcart')。submit(function(e){
 add($(this)); 
 clearoption(); 
 e.preventDefault  (); 
}); 
   
 
 

它正在调用函数来清除#my-item-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

    打赏 评论

相关推荐 更多相似问题