$(function () { $("#code").keyup(function () { $.ajax({ url:"checkCodeIsRight.action", data:"code=" + $("#code").val(), success:function (result) { if (result == "true"){ $("input[type = 'submit']")[0].disabled = false; }else { $("input[type = 'submit']")[0].disabled = true; } } }) }) })按钮怎么写
时间: 2024-02-10 21:12:08 浏览: 25
这是一个 jQuery 匿名函数,它在文档加载完成后执行。它监听 id 为 "code" 的输入框的键盘抬起事件(keyup)并发送 Ajax 请求到 "checkCodeIsRight.action" 接口,然后根据返回结果来启用或禁用提交按钮。如果想要将这段代码应用到一个按钮上,需要将其更改为监听按钮的点击事件(click),然后将其绑定到按钮上。代码如下:
HTML:
```
<button id="myButton">按钮</button>
<input type="text" id="code">
<input type="submit" disabled>
```
JavaScript:
```
$(function () {
$("#myButton").click(function () {
$.ajax({
url: "checkCodeIsRight.action",
data: "code=" + $("#code").val(),
success: function (result) {
if (result == "true") {
$("input[type='submit']").prop("disabled", false);
} else {
$("input[type='submit']").prop("disabled", true);
}
}
});
});
});
```
在这个例子中,我们将代码应用到了一个 id 为 "myButton" 的按钮上,并将输入框和提交按钮添加到了页面中。当按钮被点击时,代码中的匿名函数将会被执行。它会发送 Ajax 请求到 "checkCodeIsRight.action" 接口,并将输入框中的内容作为请求参数。当接口返回结果时,代码会根据结果来启用或禁用提交按钮。注意,这里使用了 prop() 方法来修改按钮的 disabled 属性,这是 jQuery 1.6+ 中推荐使用的方法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
翻译一下这个代码:layui.use(['jquery','table'], function(){ var table = layui.table; var $ = layui.$ //重点处 $("#keyword").keyup(function () { $.ajax({ url: "/hotwords/getHotWords", success: function(response){ $("#hotWord").empty() for (let i = 0; i < response.length; i++) { {$("#hotWord").append('<q style="font-color:red">'+response[i].hotWord+'</q>')} } }, }); }); table.render({ elem: '#test' ,url:'/analyze/searchByContent' ,where:{"keyword":$("#keyword").val()} ,toolbar: '#toolbarDemo' //开启头部工具栏,并为其绑定左侧模板 ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义,去除该参数即可 title: '提示' ,layEvent: 'LAYTABLE_TIPS' ,icon: 'layui-icon-tips' }] ,id: 'testReload' ,title: '爬取内容结果' ,cols: [[ {type: 'checkbox', fixed: 'left'} ,{field:'id', title:'排名', width:200, fixed: 'left', unresize: true, sort: true} ,{field:'content', title:'内容', width:300, edit: 'text'} ,{field:'url', title:'访问路径', width:300, edit: 'text',templet: function(res){ return ''+ res.url +'' } } ]] ,page: true });
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)