Javascript 连接JQuery变量
我需要在每个javascript变量和JQuery元素的末尾添加一个数字 这是我的密码:Javascript 连接JQuery变量,javascript,jquery,variables,Javascript,Jquery,Variables,我需要在每个javascript变量和JQuery元素的末尾添加一个数字 这是我的密码: $("#insert1").click(function(){ var collegeId1=$("#collegeId1").val(); $.post('insert.php', {collegeId: collegeId1}); return false; }); $("#insert2").click(function(){ var collegeId2=$("#
$("#insert1").click(function(){
var collegeId1=$("#collegeId1").val();
$.post('insert.php', {collegeId: collegeId1});
return false;
});
$("#insert2").click(function(){
var collegeId2=$("#collegeId2").val();
$.post('insert.php', {collegeId: collegeId2});
return false;
});
。。。等等我需要继续在每个元素和变量的末尾添加数字(即collegeId[number]和(“#insert[number]”)。我尝试了循环、.append、+等,但似乎没有任何效果
任何帮助都将不胜感激!如果您必须使用已获得的标记,请尝试此
$('[id^="insert"]').filter(function() {
return /insert\d+$/.test(this.id); // filter to make sure the id matches /insert\d+/
}).on('click', function(e) {
e.preventDefault();
var i = /\d+$/.exec(this.id)[0], // fetch index from id
collegeId = $('#collegeId' + i).val();
$.post('insert.php', {collegeId: collegeId});
})';
如果必须使用已有的标记,请尝试以下操作
$('[id^="insert"]').filter(function() {
return /insert\d+$/.test(this.id); // filter to make sure the id matches /insert\d+/
}).on('click', function(e) {
e.preventDefault();
var i = /\d+$/.exec(this.id)[0], // fetch index from id
collegeId = $('#collegeId' + i).val();
$.post('insert.php', {collegeId: collegeId});
})';
正确的方法是使用“数据”字段 您有如下按钮:
<button data-id="1" class="insert">
此外,您还可以将collegeId存储在按钮标记中(不要写入“data collegeId”,因为数据字段不支持大写):
正确的方法是使用“数据”字段 您有如下按钮:
<button data-id="1" class="insert">
此外,您还可以将collegeId存储在按钮标记中(不要写入“data collegeId”,因为数据字段不支持大写):
在像php$$var;这样的javascript中不可能,你应该使用数组而不是变量名。请也发布html。似乎你正在使它变得更复杂。发布你的html代码。在像php$$var;这样的javascript中不可能,你应该使用数组而不是变量名。请也发布html。似乎你正在使它更复杂。发布你的html代码。
var collegeId = $(this).data('collegeid');