【发布时间】:2021-01-30 02:43:38
【问题描述】:
我有很多对文本字段和提交按钮,id 提交按钮和class 文本字段相同但每对不同。所以我想将按钮单击后在文本字段中输入的值传递给 ajax 函数。
function update_rate(id){
// var price = document.getElementsByClassName(id)[0].innerHTML;
var price = document.getElementsByClassName(id);
console.log(price);
$.ajax({
type: "POST",
url: "update_rate.php", // Name of the php files
data: {subcategory : id , price: price},
success: function(res)
{
// console.log(html);
alert(res);
}
});
}
first pair:
<div class="form-group">
<input type="text" class="form-control" name="a" placeholder="Your task rate excl. taxes">
</div>
<button type="submit" id="a" onclick="update_rate(this.id)" class="btn btn-primary">Update</button>
<div class="form-group">
<input type="text" class="form-control" name="b" placeholder="Your task rate excl. taxes">
</div>
<button type="submit" id="b" onclick="update_rate(this.id)" class="btn btn-primary">Update</button>
但我无法将文本字段的值放入变量中。
【问题讨论】:
-
这里 id 是函数的参数,也是文本字段的类名,那么如何在文本字段中输入值?
-
请参阅下面的答案,了解更简单的方法。
标签: javascript html jquery ajax