请检查下面的代码。我想抓住点击基础输入字段值。例如,当点击“保存”按钮时,它将获取其输入值,如“john”& “pohn”并从名为CategoryName的变量显示到console.log()。代码贝娄无法正确获取值。提前谢谢
HTML:
<input class="form-control" id="editName" value="john"/>
<button type="button" class="btn btn-default Save" name-value="05" id-value="01">Save</button>
<input class="form-control" id="editName" value="pohn"/>
<button type="button" class="btn btn-default Save" name-value="06" id-value="02">Save</button>
jquery的:
<script>
$(document).ready(function () {
$(".Save").on("click", function () {
var id = $(this).attr("id-value");
var CategoryName = $(this).attr("id").valueOf();
console.log(CategoryName);
});
});
</script>
答案 0 :(得分:1)
这是所需的输出吗?
注意:不要将具有相同ID的两个元素命名为
$(document).ready(function () {
$(".Save").on("click", function () {
var id = $(this).attr("id-value");
var CategoryName = $(this).prev("input[id=editName"+id+"]").val();
console.log(CategoryName);
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="form-control" id="editName01" value="john"/>
<button type="button" class="btn btn-default Save" name-value="05" id-value="01">Save</button>
<input class="form-control" id="editName02" value="pohn"/>
<button type="button" class="btn btn-default Save" name-value="06" id-value="02">Save</button>
&#13;