将数据提交到单个变量中
我将表单中的一些值传递到函数中,以便为数据库插入做好准备。 该功能理论上工作正常。然而,它错过了一个独特的价值,这给我带来了麻烦。 我正在尝试根据提交到数据库的数据生成唯一 ID。 我想像这样生成ID:
var mystringId = prodID+'{'+selectId+'}'+selectedOptionValue;
应输出例如:20{100}3{200}6。 在我的示例中,它会生成以下内容:
20{100}3
20{200}6,
因为 each();
$("#submitme").live('click', function() {
//var mystringId = prodID;
$('select').each(function() {
var addedOn = new Date();
var selectName= $(this).attr("name");
var selectId= $(this).attr("id");
var selectedOption = $(this).find('option:selected');
var selectedOptionValue = selectedOption.val();
var selectedOptionText = selectedOption.text();
var mystringId = prodID+'{'+selectId+'}'+selectedOptionValue;
db.transaction(function(tx) {
tx.executeSql('INSERT INTO xxxxx mystringId');
});
});
$.mobile.changePage( "#cart", { transition: "slideup"} );
});
I am passing some values out of a form into a function so it is ready for database insertion.
The function theoretically works fine. However, it misses a unique value that puts me in trouble afterward.
I'm trying to generate a unique ID based on the data that is submitted to the database.
I want to generate the ID like this:
var mystringId = prodID+'{'+selectId+'}'+selectedOptionValue;
Should output for example: 20{100}3{200}6.
In my sample it generates above the following:
20{100}3
20{200}6
because of the each();
$("#submitme").live('click', function() {
//var mystringId = prodID;
$('select').each(function() {
var addedOn = new Date();
var selectName= $(this).attr("name");
var selectId= $(this).attr("id");
var selectedOption = $(this).find('option:selected');
var selectedOptionValue = selectedOption.val();
var selectedOptionText = selectedOption.text();
var mystringId = prodID+'{'+selectId+'}'+selectedOptionValue;
db.transaction(function(tx) {
tx.executeSql('INSERT INTO xxxxx mystringId');
});
});
$.mobile.changePage( "#cart", { transition: "slideup"} );
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我从您的问题中收集到的信息,您可以简单地在当前代码之前执行每个循环,以您希望在代码中使用的格式生成
mystringId
。代码
结果
结果将为 mystringId,以 prodID 的值开始
mystringId = 20
循环的第一次迭代将附加
{ 100}3
导致mystringId = 20{100}3
循环的第二次迭代将附加
{200}6
导致mystringId = 20{100}3{100}6
From what I can gather from your question, you can simply do a each loop before your current code to generate the
mystringId
in the format your looking for to use inside your code.Code
Results
The results would be mystringId starting off with the value of prodID
mystringId = 20
The first iteration of the loop would append
{100}3
resulting inmystringId = 20{100}3
The second iteration of the loop would append
{200}6
resulting inmystringId = 20{100}3{100}6