document.myform.checkbox.checked 没有表单

发布于 2024-10-18 14:22:42 字数 3905 浏览 2 评论 0原文

我知道我在问题(上面)中提到的内容是不可能的,但我这里有一个问题,我有双牛肚等,页面内的表格,我需要提交一个获取里面所有数据的表格以前的表格。

警告:我三四天前开始使用 javascript,我已经习惯了服务器端编程。

让我们看看:

javascript 检查复选框是否被选中(其中几个):

function KeepCount() {

var NewCount = 0;

if (document.iphone3g.iphone3g1.checked)
{NewCount = NewCount + 1;}

if (document.iphone3g.iphone3g2.checked)
{NewCount = NewCount + 1;}

if (document.iphone3g.iphone3g3.checked)
{NewCount = NewCount + 1;}

if (document.iphone3g.iphone3g4.checked)
{NewCount = NewCount + 1;}

if (document.iphone3gs.iphone3gs1.checked)
{NewCount = NewCount + 1;}

if (document.iphone3gs.iphone3gs2.checked)
{NewCount = NewCount + 1;}

if (document.iphone3gs.iphone3gs3.checked)
{NewCount = NewCount + 1;}

if (document.iphone3gs.iphone3gs4.checked)
{NewCount = NewCount + 1;}

if (document.iphone4.iphone41.checked)
{NewCount = NewCount + 1;}

if (document.iphone4.iphone42.checked)
{NewCount = NewCount + 1;}

if (document.iphone4.iphone43.checked)
{NewCount = NewCount + 1;}

if (document.iphone4.iphone44.checked)
{NewCount = NewCount + 1;}

if (NewCount > 1){
    descontox = 20/100;
    valorx = (total.value * descontox).toFixed(2);
    valor.value  = (total.value - valorx).toFixed(2);
    }

if (NewCount <= 1){
    valor.value = ("");}
}

这告诉我是否应该应用折扣,如果选择了多个复选框,它将应用 20% 的折扣。

如果您注意到,我有几个“if (document.iphone3g.iphone3g1.checked) { do something}”与此交互:

    <div id="one" class="hiddenDiv">

            <div class="image">
                <img class="large" src="images/iphone3g.png" alt="iphone3g" width="141" height="141" />
            </div>
            <form name="iphone3g">
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3g1" value="50.00"> Vidro Partido<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3g2" value="59.00"> LCD Danificado<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3g3" value="80.00"> Substituir capa traseira<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3g4" value="38.00"> Botão Volume
            </form>
    </div>
            <div id="two" class="hiddenDiv">

            <div class="image">
                <img class="large" src="images/iphone3gs.png" alt="iphone3g" width="141" height="141" />
            </div>
    <form name="iphone3gs">
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3gs1" value="60"> Vidro Partido 3GS<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3gs2" value="69"> LCD Danificado 3GS<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3gs3" value="89"> Substituir capa traseira 3GS<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3gs4" value="45"> Botão Volume3GS

    </form>
    </div>
            <div id="three" class="hiddenDiv">

            <div class="image">
                <img class="large" src="images/iphone4.png" alt="iphone3g" width="141" height="141" />
            </div>
    <form name="iphone4">
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone41" value="169"> Vidro/LCD Partido<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone42" value="99"> Substituir capa traseira<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone43" value="79"> Botão Volume<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone44" value="76"> Botão Home

    </form>
    </div>

我有超过 3 个,我该怎么做才能解决这个问题?我需要提交这些数据到一个 php 文件。

i know that what i mentioned on the question (above) isn't possible, but i have a problem here, i have double tripe and so on, forms inside a page, and i need to submit a form that gets all the data inside the previous forms.

WARNING: I started with javascript like 3 or 4 days ago, i am used to the server-side programming.

Let's see:

javascript checks if checkboxes are checked (Several of them):

function KeepCount() {

var NewCount = 0;

if (document.iphone3g.iphone3g1.checked)
{NewCount = NewCount + 1;}

if (document.iphone3g.iphone3g2.checked)
{NewCount = NewCount + 1;}

if (document.iphone3g.iphone3g3.checked)
{NewCount = NewCount + 1;}

if (document.iphone3g.iphone3g4.checked)
{NewCount = NewCount + 1;}

if (document.iphone3gs.iphone3gs1.checked)
{NewCount = NewCount + 1;}

if (document.iphone3gs.iphone3gs2.checked)
{NewCount = NewCount + 1;}

if (document.iphone3gs.iphone3gs3.checked)
{NewCount = NewCount + 1;}

if (document.iphone3gs.iphone3gs4.checked)
{NewCount = NewCount + 1;}

if (document.iphone4.iphone41.checked)
{NewCount = NewCount + 1;}

if (document.iphone4.iphone42.checked)
{NewCount = NewCount + 1;}

if (document.iphone4.iphone43.checked)
{NewCount = NewCount + 1;}

if (document.iphone4.iphone44.checked)
{NewCount = NewCount + 1;}

if (NewCount > 1){
    descontox = 20/100;
    valorx = (total.value * descontox).toFixed(2);
    valor.value  = (total.value - valorx).toFixed(2);
    }

if (NewCount <= 1){
    valor.value = ("");}
}

This tells me if a discount should be applied or not, if more than one checkbox is selected it will apply a 20% discount.

If you notice, i have several "if (document.iphone3g.iphone3g1.checked) { do something} that interacts with this:

    <div id="one" class="hiddenDiv">

            <div class="image">
                <img class="large" src="images/iphone3g.png" alt="iphone3g" width="141" height="141" />
            </div>
            <form name="iphone3g">
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3g1" value="50.00"> Vidro Partido<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3g2" value="59.00"> LCD Danificado<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3g3" value="80.00"> Substituir capa traseira<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3g4" value="38.00"> Botão Volume
            </form>
    </div>
            <div id="two" class="hiddenDiv">

            <div class="image">
                <img class="large" src="images/iphone3gs.png" alt="iphone3g" width="141" height="141" />
            </div>
    <form name="iphone3gs">
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3gs1" value="60"> Vidro Partido 3GS<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3gs2" value="69"> LCD Danificado 3GS<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3gs3" value="89"> Substituir capa traseira 3GS<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone3gs4" value="45"> Botão Volume3GS

    </form>
    </div>
            <div id="three" class="hiddenDiv">

            <div class="image">
                <img class="large" src="images/iphone4.png" alt="iphone3g" width="141" height="141" />
            </div>
    <form name="iphone4">
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone41" value="169"> Vidro/LCD Partido<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone42" value="99"> Substituir capa traseira<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone43" value="79"> Botão Volume<br />
    <input onclick="clickCh(this);KeepCount();" type="checkbox" name="iphone44" value="76"> Botão Home

    </form>
    </div>

I have more than 3 of those, what can i do to solve the question? i need to submit these data to a php file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天煞孤星 2024-10-25 14:22:42

使用 jQuery,这可以非常简洁地完成。

// select all checked checkboxes with name attribute that starts with 'iphone'
var NewCount = $("input[name^='iphone']:checked").length;

if (NewCount > 1){
    descontox = 20/100;
    valorx = (total.value * descontox).toFixed(2);
    valor.value  = (total.value - valorx).toFixed(2);
    }

if (NewCount <= 1){
    valor.value = ("");}
}

编辑:souza - 根据您的评论,您似乎已经通过使用单个表单元素解决了该问题。但是,您最好使用一种更通用的方法,例如上面的方法或 mrtsherman 发布的方法。按名称检查每个单独的输入元素将导致代码维护噩梦。

每次出现新的 iPhone 版本和/或运营商、平台时,都需要更新您的标记和 JavaScript。更通用的方法将允许您使用相同的命名约定简单地向标记添加一个新的复选框,并且您的 javascript 将简单地处理它,而不需要对代码进行任何更改。

Using jQuery, this could be done very succinctly.

// select all checked checkboxes with name attribute that starts with 'iphone'
var NewCount = $("input[name^='iphone']:checked").length;

if (NewCount > 1){
    descontox = 20/100;
    valorx = (total.value * descontox).toFixed(2);
    valor.value  = (total.value - valorx).toFixed(2);
    }

if (NewCount <= 1){
    valor.value = ("");}
}

EDIT: souza -- It appears based on your comments that you have already solved the issue by using a single form element. However, you would be wise to use a more generic approach like the one above, or the one posted by mrtsherman. Checking each individual input element by name will lead to a code maintenance nightmare.

It would require updating both the your markup and your javascript every time a new iPhone version and/or carrier, platform comes around. A more generic approach would allow you to simply add a new checkbox to the markup using the same naming convention and your javascript will simply handle it without the need to make any changes to the code.

笑饮青盏花 2024-10-25 14:22:42

使用 Jquery。对于这样的事情很容易使用。这是一个可以正常工作的 jsfiddle

$(document).ready(function() {
   $("input:checkbox").click(function() {
      var checked = $("input:checked").length;
      alert(checked);
   });
});

Use Jquery. Easy to use for something like this. Here is a jsfiddle with it working.

$(document).ready(function() {
   $("input:checkbox").click(function() {
      var checked = $("input:checked").length;
      alert(checked);
   });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文