简体   繁体   English

在Jquery mobile中选择选项中的问题

[英]Issue in select option in Jquery mobile

I have a table which contains input text and select option and button.The table row is cloned when the button is clicked. 我有一个包含输入文本和选择选项和按钮的表。单击按钮时克隆表行。 Every thing is working fine except the select option. 除了select选项之外,每件事都运行正常。 After the table row is cloned the select option not display what i select. 克隆表行后,选择选项不显示我选择的内容。 Here is the JsFiddle http://jsfiddle.net/aravinth/Ad22d/ 这是JsFiddle http://jsfiddle.net/aravinth/Ad22d/

Javascript code like 像Javascript代码一样

var b = 1;

function cloneRow() {

var row = document.getElementById("table");
var table = document.getElementById("particulars");

var clone = row.rows[1].cloneNode(true);
var clones = row.rows.length;

var workerName = clone.cells[0].getElementsByTagName('input')[0];
var position = clone.cells[2].getElementsByTagName('select')[0];
var date1 = clone.cells[3].getElementsByTagName('input')[0];
var fromHr = clone.cells[4].getElementsByTagName('input')[0];
var toHr = clone.cells[5].getElementsByTagName('input')[0];
var add = clone.cells[1].getElementsByTagName('input')[0];

workerName.id = "workerName" + b;
position.id = "position" + b;
date1.id = "date1" + b;
fromHr.id = "fromHr" + b;
toHr.id = "toHr" + b;
add.id = "add" + b;
$(date1).datebox();
table.appendChild(clone);
b++;

}

Also i referred this 我也提到了这个

1 . 1。 Change Select value using jQuery Uniform.js 使用jQuery Uniform.js更改选择值

2 . 2。 jquery cloning a block of element. jquery克隆一块元素。 select element acting weired 选择要素的元素

3. select not working after .clone 3.选择.clone之后不工作

but not get success. 但没有成功。 Please suggest some solutions. 请提出一些解决方案。

It seems, jQuery mobile doesn't recognize the cloned selectmenu . 看来,jQuery mobile无法识别克隆的selectmenu

What you can do, is remove the selectmenu and re-add only the HTML select and then initialize it with selectmenu() 你可以做的是删除selectmenu并重新添加HTML select然后用selectmenu()初始化它

$('.ui-select', clone).remove();
clone.cells[2].appendChild(position);
$(position).selectmenu();

See modified JSFiddle 见修改后的JSFiddle

Update: 更新:

I just followed jquery cloning a block of element. 我只是跟着jquery克隆了一块元素。 select element acting weired and found @malko's answer , which is a lot more elegant than removing and reinserting. 选择行动的元素并找到@ malko的答案 ,这比删除和重新插入要优雅得多。 This reduces it to 这减少了

$(position).closest('.ui-select').replaceWith(position);
$(position).selectmenu();

See JSFiddle JSFiddle

I think you solved select option issue by using this answer. 我认为您通过使用答案解决了选择选项问题。 And another one issue you need to fix in your fiddle. 还有一个问题,你需要解决你的小提琴。 The issue is time picker for last two columns (fromHr and toHr). 问题是最后两列(fromHr和toHr)的时间选择器。

To fix this you need to add the bellow lines in your javascript code. 要解决此问题,您需要在javascript代码中添加波纹管线。

$(fromHr).datebox();
$(toHr).datebox();

because those rows are dynamically created. 因为这些行是动态创建的。 So you need to add the above lines to show time picker in your fromHr and toHr. 所以你需要添加上面的行来显示你的fromHr和toHr中的时间选择器。

See this working FIDDLE 看到这个有效的FIDDLE

The issue is that jQuery Mobile recreates a lot of elements, for example your select, to be non-native widgets, and then binds functions to certain events. 问题是jQuery Mobile将很多元素(例如你的select)重新创建为非本机小部件,然后将函数绑定到某些事件。 When you just clone the row, you aren't getting the event-bindings, so what you need to do is actually append the raw html -- what it was before it got re-rendered -- and then trigger the create method: 当你只是克隆行时,你没有得到事件绑定,所以你需要做的是实际附加原始html - 重新渲染之前它是什么 - 然后触发create方法:

var template="<tr><td>..your row here..</td></tr>";
$("#particulars").append(template).parent().trigger('create');

I have a barely working fiddle, but I removed a lot of your code so I could easily illustrate what I am talking about. 我有一个勉强工作的小提琴,但我删除了很多你的代码,所以我可以很容易地说明我在说什么。 http://jsfiddle.net/Ad22d/81/ http://jsfiddle.net/Ad22d/81/

我有同样的问题并通过在克隆之前调用原始选择框上的selectmenu(“destroy”)来修复它,然后在附加克隆选择之后通过调用selectmenu()重新初始化选择框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM