layer.open打开的表单自动提交和textaera无法换行

提问 未结 0 95
天天飘
天天飘 2019-11-9
悬赏:20飞吻
版本:layui 2.5.5 浏览器:
已解决,是我自己用<textarea>标签错误
如题:遇到两个问题
1:layer.open打开的页面按回车键自动提交,通过判断按键return false 解决,兼容性还有问题
2:layer.open打开的表单中textaera无法换行,通过js更改value ,添加“\r\n” 仍不换行 (chrome中console.log显示换行),且js更改的textarea值,浏览器调试中未改变


index.html中利用layer打开edit。html

 layer.open({type:2,
title:'编辑配置',
content:'{:url("edit")}'+'?id='+data.id,shade: 0.5,
area:[$(window).width()*0.95+'px', $(window).height()+'px'],
});
edit.html
<form class="layui-form" id="add_config" action="" lay-filter="example" >





<div class="layui-form-item">
<label class="layui-form-label">默认值</label>
<div class="layui-input-block">
<input type="textarea" name="value" placeholder="请输入默认值" value="{$res.value}" autocomplete="off" class="layui-input" lay-verify="required">

</div>
</div>




<div class="layui-form-item">
<div class="layui-input-block">
<button type="submit" class="layui-btn" lay-submit="" id="submit" lay-filter="demo1" autofocus="false">更新</button>
<button type="button" class="layui-btn layui-btn-normal" id="LAY-component-form-setval">重置</button>
</div>
</div>
</form>

</fieldset>
{/block}

{block name="javascript"}
<script>
layui.use(['form', 'layedit', 'laydate','jquery','table'], function() {
var form = layui.form
, layer = layui.layer
, table = layui.table
, layedit = layui.layedit
, laydate = layui.laydate
, $ = layui.jquery;
form.render();



//表单赋值
layui.$('#LAY-component-form-setval').on('click', function(){
form.val('example', {
"name": ''
,"title": ""
,"display_type": 1
,"group": ''
,"option": ''
,"value": ""
,"listorder": 0
,"status": 0
,"remark": '说点什么吧'
});
});


document.onkeydown = function(e){

var ev =document.all ? window.event : e;
if(ev.keyCode==13) {
//兼容chrome和firefox
elem=e.path[0]?e.path[0]:e.originalTarget;
var str=$(elem).val();
var appendStr = '/n';
var index = getCursortPosition(elem); //获取当前光标所在位置并在其后添加“/n ,/r/n”均不换行
finalStr = `${str.substr(0, index)}${appendStr}${str.substr(index)}`;
console.log(finalStr);
$(elem).val( finalStr);
return false;
}
}
function getCursortPosition(element) {
var CaretPos = 0;
if (document.selection) {
//支持IE
element.focus();
var Sel = document.selection.createRange();
Sel.moveStart('character', -element.value.length);
CaretPos = Sel.text.length;
}else if (element.selectionStart || element.selectionStart == '0')
//支持firefox
CaretPos = element.selectionStart;
return (CaretPos);
}
</script>
{/block}
回帖
  • 消灭零回复