Home > Backend Development > PHP Tutorial > javascript - 怎么在点击确定按钮后退出 input 编辑状态,并且按钮恢复到修改按钮

javascript - 怎么在点击确定按钮后退出 input 编辑状态,并且按钮恢复到修改按钮

WBOY
Release: 2016-06-06 20:33:39
Original
1448 people have browsed it

<code><div class="row cl">
<label class="form-label col-3">测试:</label>
<div class="formControls col-6">
<label><span id="OrderTime">待修改文字</span></label>
</div>
<input id="changeOrderTime" type="button" value="修改" onclick="changeOrderTime()">
<button style="display:none" id="timeChangeOK">确定</button>
<script type="text/javascript">
function changeOrderTime(){
document.getElementById('timeChangeOK').style.display='block'; 
document.getElementById('changeOrderTime').style.display='none';
var o = document.getElementById("OrderTime");
var c = o.innerHTML;
o.innerHTML = "<input type='text' value='" + c + "'/>"
}
</script>
</div>
</code>
Copy after login
Copy after login

现在这段代码是可以在点击修改后 按钮变为确定,并且input输入框会出现

回复内容:

<code><div class="row cl">
<label class="form-label col-3">测试:</label>
<div class="formControls col-6">
<label><span id="OrderTime">待修改文字</span></label>
</div>
<input id="changeOrderTime" type="button" value="修改" onclick="changeOrderTime()">
<button style="display:none" id="timeChangeOK">确定</button>
<script type="text/javascript">
function changeOrderTime(){
document.getElementById('timeChangeOK').style.display='block'; 
document.getElementById('changeOrderTime').style.display='none';
var o = document.getElementById("OrderTime");
var c = o.innerHTML;
o.innerHTML = "<input type='text' value='" + c + "'/>"
}
</script>
</div>
</code>
Copy after login
Copy after login

现在这段代码是可以在点击修改后 按钮变为确定,并且input输入框会出现

给你动态生成的input绑定一个keyup或者keydown事件。我都做法是监听回车键Enter

<code>// 首先给你动态生成的 input 给一个 id 或者 class ,方便获取 dom 。
o.innerHTML = "<input type="text" id="demo" value='" + c + "'>"

function changeOrderTime(){
    document.getElementById('timeChangeOK').style.display='block'; 
    document.getElementById('changeOrderTime').style.display='none';
    var o = document.getElementById("OrderTime");
    var c = o.innerHTML;
    o.innerHTML = "<input type="text" id="demo" value='" + c + "'>"
}
timeChangeOK.addEventListener('click', function(){
    var o = document.getElementById("OrderTime");;
    o.innerHTML = document.getElementById('demo').value;

    document.getElementById('timeChangeOK').style.display='none'; 
    document.getElementById('changeOrderTime').style.display='block';
});
</code>
Copy after login

用了jquery简单的写了一下,这里是提前把 input写到 html里面,在js控制显示或者隐藏操作。

<code><div class="row cl">
<label class="form-label col-3">测试:</label>
<div class="formControls col-6">
<label><span id="OrderTime">待修改文字</span>
    <input type="text" id="OrderTimeInput" style="display:none">
</label>
</div>
<input id="changeOrderTime" type="button" value="修改" onclick="changeOrderTime()">
<button style="display:none" id="timeChangeOK">确定</button>
<script type="text/javascript">
function changeOrderTime(){
    var spanValue = $("#OrderTime").text();
    $("#OrderTimeInput").val(spanValue);
    $("#OrderTimeInput").show();
    $("#OrderTime").hide();
    $("#changeOrderTime").hide();
    $("#timeChangeOK").show();
}
$("#timeChangeOK").click(function(){
    var inputValue = $("#OrderTimeInput").val();
    $("#OrderTime").text(inputValue);
    $("#OrderTimeInput").hide();
    $("#OrderTime").show();
    $("#changeOrderTime").show();
    $("#timeChangeOK").hide();
});
</script>


                            </div></code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template