準備:將jquery-1.3.2.min.js放到/luci-static/resources/目錄下
1. 新建view
/usr/lib/lua/luci/view/test目錄下新建setHostname.htm檔案

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SET-HOSTNAME</title>
<script type="text/javascript" src="https://bbs.csdn.net/luci-static/resources/jquery-1.3.2.min.js"></script>
</head>
<script type="text/javascript">
function login(){
var name = $('#txt1').val();
//alert(name);
$.ajax({
url:'hostname',
dataType:'json',
method:'post',
data:{hostname:name},
success:function(r){
alert(r.data);
alert(r.msg);
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
}
$(function(){
alert("ok");
});
</script>
<body>
<p>Setting the IP address</p>
<form method="get" action="<%=luci.dispatcher.build_url("admin","test", "hostname")%>">
<input type="text" id="txt1" name="hostname">
<button id="submit">......</button>
</form>
<input style="width:80px" type="button" value="https://bbs.csdn.net/topics/dpost" onclick="login()"/>
</body>
</html>
2. 新建controller
/usr/lib/lua/luci/controller/admin目錄下新建test.lua

module("luci.controller.admin.test", package.seeall)
function index()
entry({"admin", "test"}, alias("admin", "test", "test"), _("Test1"), 30).index = true
entry({"admin", "test", "control"}, cbi("admin_test/control"), _("ControlTest"), 1)
entry({"admin","test", "hello"}, template("test/hello"), _("HelloTest"), 2).dependent = false
entry({"admin", "test", "test-cbi"}, cbi("admin_test/test-cbi"), _("test-cbi"), 3).dependent = false
entry({"admin", "test", "mytest"}, template("test/setHostname"),_("mytest"),4).dependent = false
entry({"admin","test", "hostname"}, call("set_hostname"))
end
function set_hostname()
local hostname = luci.http.formvalue("hostname")
local executeString = "uci set test.@controlboard[0].hostname="..hostname
luci.sys.exec(executeString)
luci.sys.exec("uci commit test");
local o = {}
o['data'] = "ok"
o['msg'] = 'success'
luci.http.prepare_content("application/json")
luci.http.write_json(o)
end
3. 驗證
/etc/config/目錄下的test檔案hostname屬性默認顯示OpenWrt

進入mytest頁面


文本框輸入cbox并點擊“dpost”按鈕提交

查看/etc/config/目錄下的test檔案的hostname屬性更改為cbox了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/111394.html
標籤:無線
上一篇:OTC 焊接機器人編程問題
