将文本更改为输入并返回文本
change text to input and back to text
我有一个产品列表,价格是普通文本。当我点击价格时,我想用输入替换文本,当我调用 blur 事件时,它会更新数据库数据并用文本替换输入。
我有这个代码:
<td>
<span onclick="make_field(this, 'text', '11', '12,350.00')">12,350.00</span> €
</td>
<script>
function make_field(el, type, id, val) {
var el = $(el);
el.html('<input onblur="update_db_row(params); make_el(this, ''span'', ' + id + ', ''' + val + ''', ''' + **this.value** + ''')" type=' + type + ' name=' + id + ' value="' + val.replace(',',' ').replace(',',' ').replace('.',',') + '">'); // replace 12,350.00 to 12350,00
el.attr('onclick', '');
}
function make_el(el, tag_name, id, old_value, **new_value**) {
var el = $(el);
alert(old_value);
alert(new_value); // undefined, how I can give current input value to function when I click outside the input?
}
</script>
谢谢。
我相信
这就是你想要的: jsFiddle 示例
您可以直接从输入元素获取新值,如下所示:
function make_field(el, type, id, val) {
var el = $(el);
el.html('<input onblur="update_db_row(); make_el(this, ''span'', ' + id + ', ''' + val + ''')" type=' + type + ' name=' + id + ' value="' + val.replace(',', ' ').replace(',', ' ').replace('.', ',') + '">'); // replace 12,350.00 to 12350,00
el.attr('onclick', '');
}
function make_el(el, tag_name, id, old_value) {
var el = $(el);
// Get the new value directly from the input.
var new_value = el.val();
// Set the new value on the span and remove the input box.
el.parent().html(new_value);
el.remove();
}
function update_db_row() {}
相关文章:
- 如何在文本搜索后返回long/lat
- .val()不返回输入文本
- HTML如何根据javascript函数的返回值限制文本输入
- JSTree's data.rslt.obj.text()返回一个文本数组,而不是所需节点的文本
- 检查文本区域是否为空总是返回false
- PHP/Ajax/jquery/JON-在Ajax Post之后将echo文本中的一部分作为变量返回
- 如何从jQuery对象文本正确调用服务器端ASP.NET并将数据返回给jQuery
- 用Promises返回的数据替换出现的文本
- 在HTML表单中键入时返回文本
- 悬停在动画文本上后,文本淡入淡出不会返回
- 如何将答案返回到文本区域
- 如何添加 SVG 文本标记 从 JavaScript 函数返回的值
- 将JS变量作为文本返回以发送到Google Analytics
- 可以't在Chrome扩展上返回所选文本
- 如何返回jQuery对象中的文本节点
- jQuery文本返回NULL
- AJAX成功请求"消息文本"返回显示
- 如何操作regex从文本返回url数组
- jQM无文本返回按钮
- 通过组合动态和静态文本返回变量值