我正在尝试使用Phonegap开发应用程序。在这个应用程序中,我需要创建一个简单的注册表单并将其连接到db,以便在尝试运行创建的应用程序时将用户详细信息存储在db.But中我在LogCat中遇到了以下错误。我已经搜索了解决问题的方法,但无法修复它。
LogCat中的错误:
file:///android_asset/www/js/index.js:Line 41:TypeError:表达式结果' parentElement' [null]不是对象。
ReferenceError:无法找到变量:$ at file:///android_asset/www/Register.html:68
index.js文件:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening'); //line 41
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
Register.html中的第68行是insertDB(tx)函数中的以下代码。
var insertname = $('[name="name"]').val();
与注册表格相关的代码。
<div data-role="page" id="page1">
<div data-role="header" >
<h1>Register</h1>
</div>
<div data-role="content">
<form method="get" onsubmit="" >
<fieldset class="ui-field-contain">
<label for="name">Name</label>
<input type="text" name="name" id="name" value="" />
<p> </p>
<label for="email">Email</label>
<input type="email" name="email" id="email" value="" />
<p> </p>
<label for="password">Password </label>
<input type="password" name="password" id="password" value="" autocomplete="off"/>
<br />
<button class="ui-btn-right" onclick='return submitForm()'>Register</button>
</fieldset>
</form>
</div>
</div>
答案 0 :(得分:0)
在第68行,您可以将其更改为:
var insertname = $('#name').val();
或
var insertname = document.getElementsByName("name");
获得预期结果?