简体   繁体   English

验证输入字段的值是否为0,然后忽略

[英]Validating input field if value is 0 then ignore

I need help, how can I ignore the input fields if the value is "0".. in javascript 我需要帮助,如果JavaScript中的值为“ 0”,我该如何忽略输入字段。

So basically if the value is "0" do not send the the content to paypal as paypal would give an error if you send it "0" 因此,基本上,如果值是“ 0”,则不要将内容发送给贝宝,因为如果您将其发送给“ 0”,贝宝就会出错

Idea is for a user to select a product WHITE or BLACK and the quantity .. say the user only wants 2 black items and 0 white then it should only send whatever has a value of 1 or more to paypal. 想法是让用户选择白色或黑色产品,数量..说用户只想要2个黑色项目和0个白色,则它只应将1或更大的值发送给贝宝。

What I need is the code for it to work.. I'll be more than glad to send a PAYPAL donation for your CODE... 我需要的是代码才能正常工作。.我很高兴为您的CODE发送PAYPAL捐款...

Thanks for your help.. 谢谢你的帮助..

<form method="post" name="mainForm" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="george@360mix.com">
<input type="hidden" name="currency_code" value="USD">


<!--First Item-->
<p>
<input type="hidden" name="item_name_1" value="WHITE Helping Hand">
<input type="hidden" name="item_number_1" value="WHITE-HH">
<input type="hidden" name="amount_1" value="10.00">
 <select name="quantity_1" value="">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
</p>

<!--Second Item--> 
<p>
<input type="hidden" name="item_name_2" value="BLACK Helping Hand">
<input type="hidden" name="item_name_2" value="BLACK-HH">
<input type="hidden" name="amount_2" value="10.00">
 <select name="quantity_2" value="">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
</p>
<br>

<input type="hidden" name="return" value="http://www.360mix.com/thankyou.html">
<input type="image" src="http://images.paypal.com/en_US/i/btn/x-click-but22.gif" border="0" name="submit" width="87" height="23" alt="Make payments with PayPal - it's fast, free and secure!">
</form>

Well try the example below. 好吧,尝试下面的例子。 The idea is to check for every quantity if zero. 这个想法是检查每个数量是否为零。 If not, assign the next item number. 如果不是,请分配下一个项目编号。

<html>
    <head>
        <script type="text/javascript">
            function prepareForm() {
                var index = 1;

                var q1 = document.getElementById('quantity_1').value;

                if (q1 > 0) {
                    document.getElementById('item_name_1').setAttribute('name', 'item_name_' + index);
                    document.getElementById('item_number_1').setAttribute('name', 'item_number_' + index);
                    document.getElementById('amount_1').setAttribute('name', 'amount_' + index);
                    document.getElementById('quantity_1').setAttribute('name', 'quantity_' + index);
                    index++;
                } else {
                    // reset (in case back button is pressed)
                    document.getElementById('item_name_1').setAttribute('name', '');
                    document.getElementById('item_number_1').setAttribute('name', '');
                    document.getElementById('amount_1').setAttribute('name', '');
                    document.getElementById('quantity_1').setAttribute('name', '');                 
                }

                var q2 = document.getElementById('quantity_2').value;

                if (q2 > 0) {
                    document.getElementById('item_name_2').setAttribute('name', 'item_name_' + index);
                    document.getElementById('item_number_2').setAttribute('name', 'item_number_' + index);
                    document.getElementById('amount_2').setAttribute('name', 'amount_' + index);
                    document.getElementById('quantity_2').setAttribute('name', 'quantity_' + index);
                    index++;
                } else {
                    // reset (in case button is pressed)
                    document.getElementById('item_name_2').setAttribute('name', '');
                    document.getElementById('item_number_2').setAttribute('name', '');
                    document.getElementById('amount_2').setAttribute('name', '');
                    document.getElementById('quantity_2').setAttribute('name', '');                 
                }

                if (index == 1) {
                        // in index is still one, then all quantities are zero
                        return false; // do not submit
                }

                return true;
            }
        </script>
    </head>
    <body>
<form method="post" name="mainForm" action="https://www.paypal.com/cgi-bin/webscr" onSubmit="return prepareForm();">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="george@360mix.com">
<input type="hidden" name="currency_code" value="USD">


<!--First Item-->
<p>
<input type="hidden" id="item_name_1" value="WHITE Helping Hand">
<input type="hidden" id="item_number_1" value="WHITE-HH">
<input type="hidden" id="amount_1" value="10.00">
 <select id="quantity_1">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
</p>

<!--Second Item--> 
<p>
<input type="hidden" id="item_name_2" value="BLACK Helping Hand">
<input type="hidden" id="item_number_2" value="BLACK-HH">
<input type="hidden" id="amount_2" value="10.00">
 <select id="quantity_2" value="">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
  </select>
</p>
<br>

<input type="hidden" name="return" value="http://www.360mix.com/thankyou.html">
<input type="image" src="http://images.paypal.com/en_US/i/btn/x-click-but22.gif" border="0" name="submit" width="87" height="23" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM