Javascript .replace为jquery
我正在为我正在设计的Web应用程序使用jquery的.replaceWith()函数。我的HTML是这样的:Javascript .replace为jquery,javascript,jquery,html,Javascript,Jquery,Html,我正在为我正在设计的Web应用程序使用jquery的.replaceWith()函数。我的HTML是这样的: {% for service in services %} <div id="service" name="name" value="{{service.name}}"> {{service.name}} <a href='main/name/{{service.name}}'> Click if you want to se
{% for service in services %}
<div id="service" name="name" value="{{service.name}}">
{{service.name}} <a href='main/name/{{service.name}}'> Click if you want to see more </a>
<div id="rating_services">
<form name="rating">
{% csrf_token %}
<input name="star1" id="star_1" type="radio" class="star" value="1"/>
<input name="star1" id="star_2" type="radio" class="star" value="2"/>
<input name="star1" id="star_3" type="radio" class="star" value="3"/>
<input name="star1" id="star_4" type="radio" class="star" value="4"/>
<input name="star1" id="star_5" type="radio" class="star" value="5"/>
<input type="submit" class="button" id="submit_btn" value="Rate this agency!">
</form>
{% endfor %}
</div>
{%用于服务中的服务%}
{{service.name}
{%csrf_令牌%}
{%endfor%}
我的javascript是:
$(function()
{
$(".button").click(function()
{
$('#rating_services').replaceWith("<div id='thanks'> Thanks! </div>");
}
});
$(函数()
{
$(“.button”)。单击(函数()
{
$(“#评级服务”)。替换为(“谢谢!”;
}
});
我想做的是,如果用户评分。将显示感谢而不是评分。但是,信息显示在for循环中(用于显示多个“代理”)。因此,我只能逐个评分,一次一个。我如何更改它以使评分无序?我尝试将div
id
更改为{{forloop.counter}
,但是javascript可以处理#
和{{forloop.counter}}
。有什么建议吗?我想你有多个div
ID为“rating\u services”的循环
如果是这样的话,您应该给每个div
s类(即class=“rating\u services”
而不是ID=“rating\u services”
而不是ID=“rating\u services”
),然后使用jQueryclosest()
函数动态检索div
,如下所示:
$(function()
{
$(".button").click(function()
{
$(this).closest('.rating_services').replaceWith("<div id='thanks'> Thanks! </div>");
}
);
});
$(函数()
{
$(“.button”)。单击(函数()
{
$(此)。最接近(“.rating_services”)。替换为(“谢谢!”;
}
);
});
有关最接近()函数的详细信息,请参见:
我认为由于您的循环,您有多个ID为“评级服务”的
div
s
如果是这样的话,您应该给每个div
s类(即class=“rating\u services”
而不是ID=“rating\u services”
而不是ID=“rating\u services”
),然后使用jQueryclosest()
函数动态检索div
,如下所示:
$(function()
{
$(".button").click(function()
{
$(this).closest('.rating_services').replaceWith("<div id='thanks'> Thanks! </div>");
}
);
});
$(函数()
{
$(“.button”)。单击(函数()
{
$(此)。最接近(“.rating_services”)。替换为(“谢谢!”;
}
);
});
有关最接近()函数的详细信息,请参见:
由于单击按钮,可能需要在按钮上添加id
$('input.button#'+forloop.counter).click(function()
{
$('#rating_services'+ forloop.counter).replaceWith(...
}
这可能有很多事件处理程序,所以您可能希望使用
$('div.parent_div').delegate('input.button#'+forloop.counter, ...)..
因为点击是在按钮上,也许把id放在按钮上
$('input.button#'+forloop.counter).click(function()
{
$('#rating_services'+ forloop.counter).replaceWith(...
}
这可能有很多事件处理程序,所以您可能希望使用
$('div.parent_div').delegate('input.button#'+forloop.counter, ...)..
和JS:
$(function() {
$("form[name=rating]").submit(function(e) {
e.preventDefault();
$(this).closest('.rating_services').replaceWith("<div id='thanks'> Thanks! </div>");
});
});
$(函数(){
$(“表格[名称=评级]”)。提交(功能(e){
e、 预防默认值();
$(此)。最接近(“.rating_services”)。替换为(“谢谢!”;
});
});
和JS:
$(function() {
$("form[name=rating]").submit(function(e) {
e.preventDefault();
$(this).closest('.rating_services').replaceWith("<div id='thanks'> Thanks! </div>");
});
});
$(函数(){
$(“表格[名称=评级]”)。提交(功能(e){
e、 预防默认值();
$(此)。最接近(“.rating_services”)。替换为(“谢谢!”;
});
});
不清楚您所谈论的循环类型。请编辑您的问题,以展示循环如何工作的示例-如果这意味着多个
或其他什么。很难理解您遇到的问题是什么,我没有看到for循环,您发布的代码似乎是有效的。您能详细说明您发布的实际问题吗必要的相关代码?似乎缺少一个)
在你的点击处理程序中。不清楚你在谈论什么类型的循环。请编辑你的问题,以展示循环如何工作的示例-如果这意味着多个
或其他什么。很难理解你遇到的问题是什么,我没有看到for循环,你发布的代码看起来是有效的。你能详细说明你的问题吗根据需要发布相关代码时出现实际问题?您的单击处理程序中似乎缺少一个“”;“
”。@sam在覆盖表单之前发送请求。或者如果您打算提交请求,请注释掉“preventDefault”
行。好的,谢谢。那么就在replaceWith提交Ajax请求之前?是的,Ajax或其中之一例如,您可以执行类似于$.post('urlToPost',$(this).serialize(),function(data){/*回调函数*/})的操作;
@sam在覆盖表单之前发送请求。或者,如果您打算提交请求,请注释掉preventDefault
行。好的,谢谢。那么就在replaceWith提交Ajax请求之前?是的,Ajax或它的一个缩写,例如,您可以执行类似于$.post('urlToPost',$(this)。serialize()的操作,函数(数据){/*回调函数*/});