Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将文本更改为输入并返回到文本_Javascript_Jquery_Events_Blur - Fatal编程技术网

Javascript 将文本更改为输入并返回到文本

Javascript 将文本更改为输入并返回到文本,javascript,jquery,events,blur,Javascript,Jquery,Events,Blur,我有一个产品清单,价格是正常的文字。当我用price点击时,我想用输入替换文本,当我调用blur事件时,它会更新DB数据并用文本替换输入 我有以下代码: <td> <span onclick="make_field(this, 'text', '11', '12,350.00')">12,350.00</span> € </td> <script> function make_field(el, type, id, va

我有一个产品清单,价格是正常的文字。当我用price点击时,我想用输入替换文本,当我调用blur事件时,它会更新DB数据并用文本替换输入

我有以下代码:

<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>

12,350.00 €
函数make_字段(el、类型、id、val){
变量el=$(el);
el.html(“”);//将12350.00替换为12350,00
el.attr('onclick','');
}
函数make_el(el、标记名、id、旧值、**新值**){
变量el=$(el);
警报(旧值);
alert(new_value);//未定义,当我在输入外部单击时,如何为函数提供当前输入值?
}

谢谢。

我相信这就是你想要的:

您可以直接从输入元素获取新值,如下所示:

 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() {}
函数make_字段(el、类型、id、val){
变量el=$(el);
el.html(“”);//将12350.00替换为12350,00
el.attr('onclick','');
}
函数make_el(el、标记名、id、旧值){
变量el=$(el);
//直接从输入中获取新值。
var new_value=el.val();
//在量程上设置新值并移除输入框。
el.parent().html(新的_值);
el.移除();
}
函数更新_db_row(){}

我认为您可以通过使用库来实现这一点,它非常易于使用,并且有很好的文档记录。Emilio,谢谢您的评论。。。如果我只需要一个函数,我想避免使用库。格雷格:是的,这正是我需要的。谢谢