javascript TypeError: document.getElementById(...) is null, in RSForms
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18755782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: document.getElementById(...) is null, in RSForms
提问by gayatri
TypeError: document.getElementById(...) is null , when i click to form for continue this error will generate , any one please tell me how to fix this error.
TypeError: document.getElementById(...) is null ,当我点击表单继续这个错误时会产生这个错误,请告诉我如何解决这个错误。
when i click to continue button , button call fnction submitform
当我点击继续按钮时,按钮调用功能提交表单
<script type="text/javascript">
function submitForm( )
{
var form = document.paywayForm;
var errorString = "";
var recurringURL = "https://www.payway.com.au/SignUp?ClientNumber=Q12882&&merchant_id=23660327&FirstPaymentDate=&CustomerNumber=&returnURL=&AddressRequired=true&PlanName=";
// NOTE: THIS USES A TEST MERCHANT!!
var netURL = "https://www.payway.com.au/MakePayment?BillerCode=128827&merchant_id=23660327&payment_amount=";
if( document.getElementById("RecurringRadio").checked == true )
{
if( document.getElementById("frequency1").value == null || document.getElementById("frequency1").value == "" )
{
errorString = errorString + " payment frequency";
}
if( document.getElementById("SelectAmount1").value == null || document.getElementById("SelectAmount1").value == "" )
{
errorString = errorString + " payment amount";
}
if( errorString == "" )
{
document.location = recurringURL + document.getElementById("frequency1").value + document.getElementById("SelectAmount1").value +
"&Firstname=" + document.getElementById("OneOffAmount1").value;
}
}
else if( document.getElementById("OneOffRadio").checked == true )
{
if( document.getElementById("OneOffAmount1").value == null || document.getElementById("OneOffAmount1").value == "" )
{
errorString = errorString + "payment amount ";
}
if( document.getElementById("Firstname").value == null || document.getElementById("Firstname").value == "" )
{
errorString = errorString + "name ";
}
if( document.getElementById("Surname").value == null || document.getElementById("Surname").value == "" )
{
errorString = errorString + "surname ";
}
if( document.getElementById("Address").value == null || document.getElementById("Address").value == "" )
{
errorString = errorString + "address ";
}
if( document.getElementById("Suburb").value == null || document.getElementById("Suburb").value == "" )
{
errorString = errorString + "suburb ";
}
if( document.getElementById("Postcode").value == null || document.getElementById("Postcode").value == "" )
{
errorString = errorString + "postcode ";
}
if( errorString == "" )
{
var newredirect = netURL + document.getElementById("OneOffAmount1").value +
"&information_fields=Direct_Funds,Title,Firstname,Surname,Address,Suburb,Postcode,State,Country,Phone,Mobile,Email,Bmail,PAWS&payment_reference=00041234" +
"&Direct_Funds=" + document.getElementById("Direct_Funds").value +
"&Title=" + document.getElementById("Title").value +
"&Firstname=" + document.getElementById("Firstname").value +
"&Surname=" + document.getElementById("Surname").value +
"&Address=" + document.getElementById("Address").value +
"&Suburb=" + document.getElementById("Suburb").value +
"&Postcode=" + document.getElementById("Postcode").value +
"&State=" + document.getElementById("State").value +
"&Country=" + document.getElementById("Country").value +
"&Phone=" + document.getElementById("Phone").value +
"&Mobile=" + document.getElementById("Mobile").value +
"&Email=" + document.getElementById("Email").value +
"&Bmail=" + document.getElementById("Bmail").value +
"&PAWS=" + document.getElementById("PAWS").value;
// alert(newredirect);
document.paywayForm.submit();
/* document.location.href = newredirect;*/
}
}
else
{
errorString = errorString + "You must select a donation type<br/>";
}
if( errorString != "" )
{
//alert(errorString);
var errorEl = document.getElementById("Error");
errorEl.innerHTML = "<b>The following fields must be completed: </b><br/> " + errorString + "";
//alert(errorEl.innerHTML);
}
}
</script>
回答by Praveen Lobo
Assuming you are looking for the drop down with name
as frequency
, you should be getting the element by name -
假设您正在寻找带有name
as的下拉菜单frequency
,您应该按名称获取元素 -
if( document.getElementsByName("frequency")[0] == null
|| document.getElementsByName("frequency")[0].selectedIndex == 0 )
{
/* error occuer here */
errorString = errorString + " payment frequency";
}
Since you already have the drop down on the page, the check for NULL will always be false and the selected value will be the first drop down value so the selected index will always be 0 if user doesn't change it. So, there is no point in checking for above as the element will not be null and the selected index will be 0 for frequency week
.
由于您已经在页面上有下拉菜单,因此对 NULL 的检查将始终为 false,所选值将是第一个下拉值,因此如果用户不更改所选索引,则所选索引将始终为 0。因此,检查上述内容没有意义,因为元素不会为空,并且所选的索引将为 0 频率week
。
What you should instead be doing is add another option to the select which is blank and then use the below code to see if user has selected a value for frequency of payment.
您应该做的是向选择中添加另一个空白选项,然后使用以下代码查看用户是否选择了付款频率值。
HTML should look like
HTML 应该看起来像
<select name="frequency" class="drop" style="width: 80px;">
<option value="" selected>Select frequency</option>
<option value="Weekly">Week</option>
<option value="Fortnightly">Fortnightly</option>
<option value="Monthly">Month</option>
</select>
and you JS code check should look like
你的 JS 代码检查应该看起来像
if( document.getElementsByName("frequency")[0].selectedIndex == 0 )
{
/* error occuer here - frequency not selected */
errorString = errorString + " payment frequency";
}
Hope this helps.
希望这可以帮助。
回答by vr_driver
I've experienced this frustrating problem too, and you may wish to change the order of the elements too, so that the elemnt comes before the JS. It could be that the JS can't find the element yet, because it hasn't loaded in time, or you could load it and execute your JS with a document.ready feature or similar.
我也遇到过这个令人沮丧的问题,您可能也希望更改元素的顺序,以便元素在 JS 之前出现。可能是 JS 找不到元素,因为它没有及时加载,或者您可以加载它并使用 document.ready 功能或类似功能执行您的 JS。
Typically our pages work like this:
通常我们的页面是这样工作的:
<head>
<scripts>
<body>
<elements>
<end>
Maybe if you try it like this it'll work:
也许如果你像这样尝试它会起作用:
<head>
<scripts>
<body>
<elements>
<script for elements>
<end>
回答by Shadow
Try this,
试试这个,
<script>
window.onload=function(){
if( document.getElementById("frequency1").value == null || document.getElementById("frequency1").value == "" ) /* error occuer here */
{
errorString = errorString + " payment frequency";
}
};
</script>
回答by Tasos Christidis
Internet Explorer is case insensitive as far as ID's are concerned. But not Mozilla and others. Make sure that the ID is spelled correctly in your code. Must exactly match in case.
就 ID 而言,Internet Explorer 不区分大小写。但不是 Mozilla 和其他公司。确保代码中的 ID 拼写正确。必须完全匹配以防万一。
回答by ramin omrani
This error occurs when your element is null(doesn't exist). So javascript cannot get its value.you can get the element first and then check if it is null or not and then ask for its value rather than just asking for the value directly without knowing if the element is visible on the HTML or not.
当您的元素为空(不存在)时会发生此错误。所以javascript无法获取它的值。你可以先获取元素,然后检查它是否为空,然后询问它的值,而不是直接询问该值而不知道该元素是否在HTML上可见。
element1 = document.getElementById("frequency1");
if(element1 != null)
{
//code to set the value variable and test the value not to be empty.
}