本文介绍了没有表单的document.myform.checkbox.checked的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我在问题(上面)中提到的是不可能的,但我在这里有一个问题,我有双重肚子等等,页面内的形式,我需要提交一个表单,获取所有以前的表单里面的数据。
警告:我在3或4天前就开始使用javascript,我习惯了服务器端编程。 b
$ b
让我们看看:
javascript会检查复选框是否被选中(其中有几个):
函数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.iphone3gs.iphone3gs4.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 = NewCoun t + 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 =oneclass =hiddenDiv>
< div class =image>
< img class =largesrc =images / iphone3g.pngalt =iphone3gwidth =141height =141/>
< / div>
<如果您的电子邮件地址为+,则表示您的电子邮件地址为+;如果您的电子邮件地址不是 >
< input onclick =clickCh(this); KeepCount();type =checkboxname =iphone3g2value =59.00> LCD Danificado< br />>
< input onclick =clickCh(this); KeepCount() ;type =checkboxname =iphone3g3value =80.00> Substituir capa traseira< br />
< input onclick =clickCh(this); KeepCount(); type =checkboxname =iphone3g4value =38.00> Botão卷
< / form>
< / div>
< div id =twoclass =hiddenDiv>
< div class =image>
< img class =largesrc =images / iphone3gs.pngalt =iphone3gwidth =141height =141/>
< / div>
< form name =iphone3gs>
< input onclick =clickCh(this); KeepCount(); type =checkboxname =iphone3gs1value =60> Vidro Partido 3GS< br />
< input onclick =clickCh(this); KeepCount(); type =checkboxname =iphone3gs2value =69>液晶Danificado 3GS< br />
< input onclick =clickCh(this); KeepCount(); type =checkboxname =iphone3gs3value =89> Substituteir capa traseira 3GS< br />
< input onclick =clickCh(this); KeepCount(); type =checkboxname =iphone3gs4value =45> BotãoVolume3GS
< / form>
< / div>
< div id =threeclass =hiddenDiv>
< div class =image>
< img class =largesrc =images / iphone4.pngalt =iphone3gwidth =141height =141/>
< / div>
< form name =iphone4>
< input onclick =clickCh(this); KeepCount(); type =checkboxname =iphone41value =169> Vidro / LCD Partido< br />>
< input onclick =clickCh(this); KeepCount(); type =checkboxname =iphone42value =99> Substituir capa traseira< br />
< input onclick =clickCh(this); KeepCount(); type =checkboxname =iphone43value =79> Botão卷< br />
< input onclick =clickCh(this); KeepCount(); type =checkboxname =iphone44value =76> Botão主页
< / form>
< / div>
我有三个以上的人,我能做些什么来解决这个问题?我需要将这些数据提交到一个php文件中。
解决方案 使用jQuery,可以非常简洁地完成这项工作。
//选中所有选中的复选框,其名称属性以'iphone'开头
var NewCount = $(input [name ^ = 'iphone']:检查)的长度。
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将简单地处理它,而无需对代码进行任何更改。
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.
解决方案
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.
这篇关于没有表单的document.myform.checkbox.checked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!