如何在javascript中使用for循环检查有效输入?
我已尝试过我的代码,但它不起作用,我该如何解决?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form onsubmit="return checkform(this);">
<span id="price_top"></span>
<a href="#" id="addScnt_price" style=" cursor: pointer; font-weight: bold; color: #0192B5; text-decoration: none; ">Add more</a>
<div id="p_scents_price">
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<input type="submit" name="submit" value="OK">
</form>
<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
var z = 1;
for(z=0;z<10;z++) {
if ((form.price'+z+'.value != "") && (form.price'+z+'.value < "1.5")) {
alert( "Minimum Price'+z+' $1.5" );
document.getElementById("price_top").scrollIntoView()
document.getElementById("price'+z+'").style.border = "1px solid red";
return false ;
}
else {
document.getElementById("price'+z+'").style.border = "1px solid #d5d5c5";
}
}
return true ;
}
</script>
<script>
$(function() {
var scntDiv = $('#p_scents_price');
var i = 1;
$('#addScnt_price').live('click', function() {
$('<p><label for="p_scnts_price"><input type="text" id="price'+i+'" size="20" name="price[]"/><a href="#" id="remScnt_price">Remove</a></label></p>').appendTo(scntDiv);
i++;
return false;
});
$('#remScnt_price').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
}
return false;
});
});
</script>
答案 0 :(得分:1)
您无法使用"
打开字符串,并使用'
终止该字符串。
你必须检查你的元素id引用。
例如,您需要更改:
document.getElementById("price'+z+'") ...
使用:
document.getElementById("price"+z) ...
您还必须更改此内容:
form.price'+z+'.value ...
使用
eval("form.price"+z).value ...