$(“.rootId”).click(function () { rootId=$(this).val('value'); alert(rootId); }) 获取a标签的value值失败
时间: 2024-01-02 07:04:07 浏览: 39
可能的原因是你的选择器 `$(".rootId")` 选中了多个元素,而 `.click()` 只会绑定到第一个匹配的元素上。你可以尝试使用 `.each()` 方法来遍历每个匹配的元素,并分别绑定事件,代码如下:
```
$(".rootId").each(function() {
$(this).click(function() {
var rootId = $(this).val();
alert(rootId);
});
});
```
另外,如果你的 `a` 标签的 `value` 属性为空,可以尝试使用 `data-*` 属性来获取数据。比如,你可以在 `a` 标签上添加 `data-rootid` 属性,然后使用 `$().data()` 方法来获取该属性的值:
```
<a href="#" class="rootId" data-rootid="123">Click me</a>
```
```
$(".rootId").click(function() {
var rootId = $(this).data("rootid");
alert(rootId);
});
```
相关问题
function getData() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == XMLHttpRequest.DONE) { if (xhr.status == 200) { var jsonData = JSON.parse(xhr.responseText); var root = { id: rootId, name: rootName, children: getChildren(jsonData, rootId) }; buildTree($('#tree tbody'), root.children); // 初始化 treegrid $('#tree').treegrid({ expanderExpandedClass: 'glyphicon glyphicon-minus', expanderCollapsedClass: 'glyphicon glyphicon-plus' }); if (this.readyState == 4 && this.status == 200) { username = this.responseText; alert("ajx获取用户名:" + this.responseText); } else { alert("请求失败:" + xhr.statusText); } } }; xhr.open("GET", "shouquan.php?username=" + username, true); xhr.send(); } }; xhttp.open("GET", "getdata.php", true); xhttp.send(); }我在onreadystatechange回调函数内执行主程序,但是还是无法正确执行
你的代码中存在一个问题,即在第13行(`xhr.open("GET", "shouquan.php?username=" + username, true);`)中使用了 `username` 变量,但是在此之前 `username` 变量并没有被定义。因此,此处的 `username` 值为 undefined,导致请求失败。
解决方案是将获取用户名的代码移动到 `var jsonData = JSON.parse(xhr.responseText);` 的后面,即在获取数据成功后再进行用户名的获取和授权的请求。例如:
```
if (xhr.status == 200) {
var jsonData = JSON.parse(xhr.responseText);
var root = { id: rootId, name: rootName, children: getChildren(jsonData, rootId) };
buildTree($('#tree tbody'), root.children);
// 初始化 treegrid
$('#tree').treegrid({
expanderExpandedClass: 'glyphicon glyphicon-minus',
expanderCollapsedClass: 'glyphicon glyphicon-plus'
});
// 获取用户名和授权
var xhrAuth = new XMLHttpRequest();
xhrAuth.onreadystatechange = function() {
if (xhrAuth.readyState == 4 && xhrAuth.status == 200) {
username = xhrAuth.responseText;
alert("ajx获取用户名:" + username);
// 进行授权请求
var xhrShouquan = new XMLHttpRequest();
xhrShouquan.onreadystatechange = function() {
if (xhrShouquan.readyState == 4 && xhrShouquan.status == 200) {
alert("授权成功");
}
};
xhrShouquan.open("GET", "shouquan.php?username=" + username, true);
xhrShouquan.send();
} else {
alert("请求失败:" + xhrAuth.statusText);
}
};
xhrAuth.open("GET", "getusername.php", true);
xhrAuth.send();
}
```
这样,就可以在获取数据成功后再进行其他操作,避免出现变量未定义的问题。
// 根节点ID和名称 var rootId = "N000"; var rootName = "菜单"; // 获取指定节点下的所有子节点 function getChildren(nodes, id) { var result = []; for (var i = 0; i < nodes.length; i++) { var node = nodes[i]; if (node.parentIndex === id) { node.children = getChildren(nodes, node.id); result.push(node); } } return result; } // 构建 DOM 树 function buildTree(parentNode, children) { for (var i = 0; i < children.length; i++) { var childNode = children[i]; var tr = $('<tr class="treegrid-' + childNode.id + '"><td>' + childNode.name + '</td></tr>'); parentNode.append(tr); if (childNode.children && childNode.children.length > 0) { var subTable = $('<table></table>'); tr.append(subTable); buildTree($('<tbody></tbody>').appendTo(subTable), childNode.children); } } } // 主程序 const username = 'hzx>' var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { alert("返回值:" + xhr.responseText); if (xhr.status == 200) { var jsonData = JSON.parse(xhr.responseText); }else { alert("请求失败:" + xhr.statusText); } } xhr.open("GET","shouquan.php?username=".username, true); xhr.send(); }; var root = { id: rootId, name: rootName, children: getChildren(jsonData, rootId) }; buildTree($('#tree tbody'), root.children); // 初始化 treegrid $('#tree').treegrid({ expanderExpandedClass: 'glyphicon glyphicon-minus', expanderCollapsedClass: 'glyphicon glyphicon-plus' });代码修改
感谢您分享代码!不过看到您的代码中有一些错误,我帮您修改一下:
1. 在发送 XMLHttpRequest 请求时,应该将 `xhr.open` 和 `xhr.send` 放在 `if` 语句外面。
2. 将 `var jsonData = JSON.parse(xhr.responseText);` 放在 `if` 语句内部,以避免在请求失败时出现 `jsonData` 未定义的情况。
3. 在 `const username = 'hzx>'` 中应该将大于号(>)改为等于号(=)。
4. 在 `buildTree` 函数中,应该先将 `tr` 添加到 DOM 中,再根据是否有子节点添加子表格。
修改后的代码如下所示:
```
// 根节点ID和名称
var rootId = "N000";
var rootName = "菜单";
// 获取指定节点下的所有子节点
function getChildren(nodes, id) {
var result = [];
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (node.parentIndex === id) {
node.children = getChildren(nodes, node.id);
result.push(node);
}
}
return result;
}
// 构建 DOM 树
function buildTree(parentNode, children) {
for (var i = 0; i < children.length; i++) {
var childNode = children[i];
var tr = $('<tr class="treegrid-' + childNode.id + '"><td>' + childNode.name + '</td></tr>');
parentNode.append(tr);
if (childNode.children && childNode.children.length > 0) {
var subTable = $('<table></table>');
tr.append(subTable);
buildTree($('<tbody></tbody>').appendTo(subTable), childNode.children);
}
}
}
// 主程序
const username = 'hzx=';
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
if (xhr.status == 200) {
var jsonData = JSON.parse(xhr.responseText);
var root = { id: rootId, name: rootName, children: getChildren(jsonData, rootId) };
buildTree($('#tree tbody'), root.children);
// 初始化 treegrid
$('#tree').treegrid({
expanderExpandedClass: 'glyphicon glyphicon-minus',
expanderCollapsedClass: 'glyphicon glyphicon-plus'
});
} else {
alert("请求失败:" + xhr.statusText);
}
}
};
xhr.open("GET", "shouquan.php?username=" + username, true);
xhr.send();
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)